国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 青浦区| 和平县| 息烽县| 南江县| 东至县| 怀柔区| 金湖县| 马山县| 竹山县| 呼和浩特市| 洪江市| 阿克陶县| 宕昌县| 奎屯市| 澳门| 平和县| 谷城县| 南木林县| 洛南县| 安多县| 扎赉特旗| 汉阴县| 兴城市| 紫金县| 四川省| 石门县| 长治县| 平山县| 东源县| 汕尾市| 顺平县| 郯城县| 慈利县| 青岛市| 将乐县| 瓦房店市| 惠安县| 广丰县| 牟定县| 互助| 迁安市|