C# zxing二维码写入的实例代码

时间:2021-09-01 12:23:26 


private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.textBox1.Text.Trim()))
            {
                MessageBox.Show("请输入需要转换的信息!");
                return;
            }

            string content = textBox1.Text;

            Hashtable hints= new Hashtable();  
            hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);//纠错级别
            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;    
        }

标签:zxing,二维码,写入
0
投稿

猜你喜欢

  • java信号量控制线程打印顺序的示例分享

    2023-05-09 12:27:38
  • Vue3源码解读effectScope API及实现原理

    2023-12-11 19:28:49
  • java字符串比较获取字符串出现次数的示例

    2022-03-22 16:05:39
  • Unity实现UI光晕效果(发光效果)

    2022-07-08 16:37:41
  • Android用动画显示或隐藏视图

    2023-08-05 20:07:25
  • opencv实现读取视频保存视频

    2021-08-16 15:57:50
  • Android重写View实现全新的控件

    2021-08-11 21:49:39
  • C#中38个常用运算符的优先级的划分和理解

    2022-09-23 17:12:44
  • ZooKeeper入门教程一简介与核心概念

    2022-11-24 18:36:00
  • Springboot实现图片上传功能的示例代码

    2022-08-05 22:46:44
  • 解决springboot配置logback-spring.xml不起作用问题

    2022-09-10 11:21:24
  • 盘点几种常见的java排序算法

    2023-09-17 10:13:10
  • Android仿直播类app赠送礼物功能

    2023-07-26 05:06:17
  • Java多线程的其他知识_动力节点Java学院整理

    2023-09-06 06:01:58
  • idea切换分支的时候,忽略一些无用的修改设置

    2022-01-10 04:13:37
  • Android刮刮卡功能具体实现代码

    2021-08-06 04:12:24
  • Java由浅入深刨析继承

    2023-11-23 08:04:24
  • C语言指针的图文详解

    2021-07-26 11:26:25
  • C#中C/S端实现WebService服务

    2023-10-16 06:01:43
  • Java向上转型和向下转型实例解析

    2022-08-12 01:59:28
  • asp之家 软件编程 m.aspxhome.com