国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C# zxing二維碼寫入的實(shí)例代碼

2020-01-24 03:31:32
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.textBox1.Text.Trim()))
            {
                MessageBox.Show("請(qǐng)輸入需要轉(zhuǎn)換的信息!");
                return;
            }

            string content = textBox1.Text;

            Hashtable hints= new Hashtable();  
            hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);//糾錯(cuò)級(jí)別
            hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//編碼格式

            ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300, hints);
            Bitmap bitmap = toBitmap(byteMatrix);
            pictureBox1.Image = bitmap;

            SaveFileDialog sFD = new SaveFileDialog();
            sFD.Filter = "*.png|*.png";
            sFD.AddExtension = true;
            try
            {
                if (sFD.ShowDialog() == DialogResult.OK)
                {
                    writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }


        public static void writeToFile(ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)
        {
            System.Drawing.Imaging.EncoderParameters eps = new System.Drawing.Imaging.EncoderParameters();
            eps.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
            Bitmap bmap = toBitmap(matrix);
            bmap.Save(file, format);
        }
        public static Bitmap toBitmap(ByteMatrix matrix)
        {
            int width = matrix.Width;
            int height = matrix.Height;
            Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("Purple") : ColorTranslator.FromHtml("0xFFFFFFFF"));//可以自定義顏色和背景色
                }
            }

            return bmap;    
        }

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 崇信县| 巴塘县| 通渭县| 兴海县| 德兴市| 阳西县| 德安县| 驻马店市| 施秉县| 广德县| 敦化市| 阿拉善右旗| 东源县| 昌邑市| 卢氏县| 北碚区| 宜春市| 泊头市| 新丰县| 天镇县| 南涧| 新昌县| 铜梁县| 崇州市| 开封县| 株洲市| 岳西县| 永清县| 昌都县| 兰溪市| 县级市| 抚远县| 河西区| 平远县| 弥渡县| 塘沽区| 手游| 基隆市| 游戏| 白沙| 平塘县|