当前位置: 移动技术网 > IT编程>开发语言>c# > Winform中使用zxing和Graphics实现自定义绘制二维码布局

Winform中使用zxing和Graphics实现自定义绘制二维码布局

2019年08月29日  | 移动技术网IT编程  | 我要评论
场景 zxing.dll下载 https://download.csdn.net/download/badao_liumang_qizhi/11623214 效果 实现 根据上面文章中将简单的二维码生成后,现在要调整其布局。 拖拽一个按钮,双击进入其点击事件。 这里新建了一个工具类ZxingHelp ...

场景

zxing.dll下载

效果

 

实现

根据上面文章中将简单的二维码生成后,现在要调整其布局。

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

private void button6_click(object sender, eventargs e)
        {
            //二维码内容对象
            assetentity assetentity = new assetentity() { name = "霸道",gender = "男",url = "123" };
            //使用上面生成二维码的方法获取二维码的bitmap对象
            bitmap bitmap = zxinghelper.createqrcode("霸道");
            //重新绘制二维码布局
            image img = zxinghelper.getprintpicture(bitmap, assetentity,400,400);
            //设置picturebox的图片源
            this.picturebox1.image = img;
        }

 

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

getprintpicture获取调整布局后的照片。

在此之前,先新建一个存储打印内容的实体类assetentity

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;

namespace npoitest
{
    class assetentity
    {
        private string name;
        private string gender;
        private string url;

        public string name { get => name; set => name = value; }
        public string gender { get => gender; set => gender = value; }
        public string url { get => url; set => url = value; }
    }
}

 

然后在工具类中

using system;
using system.collections.generic;
using system.drawing;
using system.drawing.drawing2d;
using system.drawing.printing;
using system.linq;
using system.text;
using system.threading.tasks;
using zxing;
using zxing.common;
using zxing.qrcode;

namespace npoitest
{
    class zxinghelper
    {
        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);
        }

        public static image getprintpicture(bitmap image, assetentity asset, int picwidth, int picheight)
        {
            //新建bitmap对象 用于返回 使用传递的参数作为宽度和高度
            bitmap printpicture = new bitmap(picwidth, picheight);
            //高度
            int height = 5;
            //新建字体
            font font = new font("黑体", 10f);
            //graphics :封装一个 gdi+ 绘图图面
            //fromimage :从指定的 system.drawing.image 创建新的 system.drawing.graphics。
            graphics g = graphics.fromimage(printpicture);
            //brush :定义用于填充图形形状(如矩形、椭圆、饼形、多边形和封闭路径)的内部的对象。
            brush brush = new solidbrush(color.black);
            //设置此 system.drawing.graphics 的呈现质量。
            g.smoothingmode = smoothingmode.highquality;
            //填加反锯齿代码效果
            g.textrenderinghint = system.drawing.text.textrenderinghint.antialias;

            int interval = 15;
            int pointx = 5;
            //用指定的位置和大小初始化 system.drawing.rectangle 类的新实例。
            rectangle destrect = new rectangle(190, 10, image.width, image.height);
            //在指定位置并且按指定大小绘制指定的 system.drawing.image 的指定部分。
            //graphicsunit.pixel: 指定给定的数据的度量值的单位。
            //drawimage :在指定的位置并且按原始大小绘制指定的image对象
            g.drawimage(image, destrect, 0, 0, image.width, image.height, graphicsunit.pixel);
            //
            height += 8;
            //用指定的位置和大小初始化 system.drawing.rectanglef 类的新实例。
            rectanglef layoutrectangle = new rectanglef(pointx, height, 260f, 85f);
            //在指定矩形并且用指定的 system.drawing.brush 和 system.drawing.font 对象绘制指定的文本字符串
            g.drawstring("姓名:" + asset.name, font, brush, layoutrectangle);

            height += interval;
            layoutrectangle = new rectanglef(pointx, height, 230f, 85f);
            g.drawstring("性别:" + asset.gender, font, brush, layoutrectangle);

            height += interval;
            layoutrectangle = new rectanglef(pointx, height, 230f, 85f);
            g.drawstring("链接:" + asset.url, font, brush, layoutrectangle);

          

            return printpicture;
        }
    }
}

 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网