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

场景

zxing.dll下载

https://download.csdn.net/download/badao_liumang_qizhi/11623214

效果

 

实现

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

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

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;
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值