Winform中使用zxing实现二维码生成(附dll下载)

2022-10-17,,,,

场景

zxing.dll下载

效果

 

实现

新建winform程序,将上面下载的zxing.dll添加到引用。

 

拖拽一个按钮,双击进入其点击事件。

 private void button5_click(object sender, eventargs e)
        {
            //二维码要存储的内容
            string codestring = "测试";
            //生成二维码并返回bitmap
            bitmap bitmap= zxinghelper.createqrcode(codestring);
            //保存图片到本地
            //bitmap.save(@"c:\users\administrator\desktop\a.png");
            //将bitmap转换成image对象
            image img = image.fromhbitmap(bitmap.gethbitmap());
            //设置picturebox的图片源
            this.picturebox1.image = img;

        }

 

然后在页面上拖拽一个picturebox用来显示照片

这里新建了一个工具类zxinghelper,调用其createqrcode方法返回生成二维码的bitmap格式。

这时如果想将其保存到本地,就使用bitmap的save()方法,参数是要保存的全路径。

如果想将照片显示在窗体上的picturebox控件上,则使用image的fromhbitmap()方法进行转换。

工具类中生成二维码的代码

 

public static bitmap createqrcode(string asset)
        {
            encodingoptions options = new qrcodeencodingoptions
            {
                disableeci = true,
                //编码
                characterset = "utf-8",
                //宽度
                width = 120,
                //高度
                height = 120
            };
            barcodewriter writer = new barcodewriter();
            writer.format = barcodeformat.qr_code;
            writer.options = options;
            return writer.write(asset);
        }

 

《Winform中使用zxing实现二维码生成(附dll下载).doc》

下载本文的Word格式文档,以方便收藏与打印。