【C#调用ZXing.Dll实现图像编码生成-二维码、条码生成】

前言

C#调用ZXing.Dll实现图像编码生成-二维码、条码生成。

案例演示

在这里插入图片描述

代码

图像编码工具类

public class ImageCodeTool
{
    /// <summary>
    /// 生成二维码码:指定内容,输出Bitmap
    /// </summary>
    public void GenQRCode(string context, out Bitmap bitmap)
    {
        string qrCodeText = context;
        var qrCodeWriter = new BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
            Options = new ZXing.Common.EncodingOptions
            {
                Width = 300,
                Height = 300
            }
        };
        Bitmap bmp = qrCodeWriter.Write(qrCodeText);
        bitmap = new Bitmap(bmp);
        bmp.Dispose();
        Console.WriteLine("二维码生成成功。");
    }

    /// <summary>
    /// 生成二维码码:指定内容,指定宽高,输出Bitmap
    /// </summary>
    public void GenQRCode(string context, out Bitmap bitmap, int width, int height)
    {
        string qrCodeText = context;
        var qrCodeWriter = new BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
            Options = new ZXing.Common.EncodingOptions
            {
                Width = width,
                Height = height
            }
        };

        Bitmap bmp = qrCodeWriter.Write(qrCodeText);
        bitmap = new Bitmap(bmp);
        bmp.Dispose();
        Console.WriteLine("二维码生成成功。");
    }

    /// <summary>
    /// 生成条码,指定内容,输出Bitmap
    /// </summary>
    public void GenBarCode(string context, out Bitmap bitmap)
    {
        // 要编码的文本
        string barcodeText = context; 
        var barcodeWriter = new BarcodeWriter
        {
            // 可以选择不同的条码格式
            Format = BarcodeFormat.CODE_128, 
            Options = new ZXing.Common.EncodingOptions
            {
                Width = 300,
                Height = 150
            }
        };
        Bitmap bmp = barcodeWriter.Write(barcodeText);
        bitmap = new Bitmap(bmp);
        Console.WriteLine("条码生成成功。");
    }

    /// <summary>
    /// 生成条码:指定内容,指定宽高,输出Bitmap
    /// </summary>
    public void GenBarCode(string context, out Bitmap bitmap, int width, int height)
    {
        string barcodeText = context; // 要编码的文本
        var barcodeWriter = new BarcodeWriter
        {
            Format = BarcodeFormat.CODE_128, // 可以选择不同的条码格式
            Options = new ZXing.Common.EncodingOptions
            {
                Width = width,
                Height = height
            }
        };
        Bitmap bmp = barcodeWriter.Write(barcodeText);
        bitmap = new Bitmap(bmp);
        Console.WriteLine("条码生成成功。");
    }

    /// <summary>
    /// 给图像添加字符
    /// </summary>
    public void AddTextToBitmap(ref Bitmap bitmap, string text,Font contextFont)
    {
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            SizeF textSize = g.MeasureString(text, contextFont);
            int textX = (bitmap.Width - (int)textSize.Width) / 2;
            int textY = bitmap.Height - (int)textSize.Height - 5; // 5 pixels above the bottom
            g.DrawString(text, contextFont, Brushes.Black, new PointF(textX, textY));
        }
    }
}

Form窗体类

 public partial class MainForm : Form
 {
     private Bitmap bitmap;              //图片
     private int width = 300;            //宽
     private int height = 300;           //高
     private Font ContextFont = new Font("Arial", 12); //字体
     private float zoomFactor = 1.0f;            //缩放因子
     
     private ImageCodeTool imageCodeTool;            //OCR工具
     public MainForm()
     {
         InitializeComponent();
         pictureBox1.Location = new Point(10, 10); // 设置初始位
         this.pictureBox1.MouseWheel += CustomMouseWheel;
         // 添加鼠标事件;
     }
     private void MainForm_Load(object sender, EventArgs e)
     {
         lbx_FontParams.Items.Clear();
         lbx_FontParams.Items.Add($"字体:{ContextFont.Name}");
         lbx_FontParams.Items.Add($"大小:{ContextFont.Size}");
         imageCodeTool = new ImageCodeTool();
     }
     /// <summary>
     /// 滚轮缩放图像
     /// </summary>
     private void CustomMouseWheel(object sender, MouseEventArgs e)
     {
         // 根据滚轮的方向调整缩放因子
         if (e.Delta > 0)
         {
             zoomFactor += 0.1f; // 放大
         }
         else if (e.Delta < 0)
         {
             zoomFactor -= 0.1f; // 缩小
             if (zoomFactor < 0.1f) zoomFactor = 0.1f; // 防止缩放因子小于0.1
         }
         // 重新绘制图像
         pictureBox1.Image = new Bitmap(bitmap, new Size((int)(bitmap.Width * zoomFactor), (int)(bitmap.Height * zoomFactor)));
     }
     /// <summary>
     /// 生成二维码
     /// </summary>
     private void btn_QRCode_Click(object sender, EventArgs e)
     {
         bitmap = null;
         if (ckbx_SpecifySize.Checked)
         {
             width = int.Parse(nudx_Width.Value.ToString());
             height = int.Parse(nudx_Height.Value.ToString());

             imageCodeTool.GenQRCode(tbx_OCR.Text, out bitmap, width, height);
         }
         else
         {
             imageCodeTool.GenQRCode(tbx_OCR.Text, out bitmap);
         }

         if (ckbx_GenContext.Checked)
         {
             imageCodeTool.AddTextToBitmap(ref bitmap, tbx_OCR.Text,ContextFont);
         }
         
         pictureBox1.Image = bitmap;
     }
     /// <summary>
     /// 生成条码
     /// </summary>
     private void btn_GenBarCode_Click(object sender, EventArgs e)
     {
         bitmap = null;
         if (ckbx_SpecifySize.Checked)
         {
             width = int.Parse(nudx_Width.Value.ToString());
             height = int.Parse(nudx_Height.Value.ToString());
             imageCodeTool.GenBarCode(tbx_OCR.Text, out bitmap, width, height);
         }
         else
         {
             imageCodeTool.GenBarCode(tbx_OCR.Text, out  bitmap);
             
         }
         if (ckbx_GenContext.Checked)
         {
             //AddTextToBitmap(ref bitmap, tbx_OCR.Text);
         }
         pictureBox1.Invalidate();
         pictureBox1.Image = null;
         pictureBox1.Image = bitmap;
     }

     /// <summary>
     /// 生成内容(字符串)
     /// </summary>
     private void ckbx_GenContext_CheckedChanged(object sender, EventArgs e)
     {
         if (ckbx_GenContext.Checked)
         {
             if (fontDialog_Context.ShowDialog()== DialogResult.OK)
             {
                 ContextFont = fontDialog_Context.Font;
                 lbx_FontParams.Items.Clear();
                 lbx_FontParams.Items.Add($"字体:{ContextFont.Name}");
                 lbx_FontParams.Items.Add($"大小:{ContextFont.Size}");
             }
         }
     }
     /// <summary>
     /// 指定大小
     /// </summary>
     private void nudx_Width_ValueChanged(object sender, EventArgs e)
     {
         if (ckbx_LockWH.Checked)
         {
             nudx_Height.Value = nudx_Width.Value;
         }
     }
     /// <summary>
     /// 指定大小
     /// </summary>
     private void nudx_Height_ValueChanged(object sender, EventArgs e)
     {
         if (ckbx_LockWH.Checked)
         {
             nudx_Width.Value = nudx_Height.Value;
         }
     }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值