使用ZXing.Net开源库生成二维码

本文介绍了如何在.NETFramework4.6.1项目中利用ZXing.Net库生成二维码,包括环境设置、界面设计以及核心代码实现,展示了如何将文本转换为二维码并显示在WindowsForms应用程序中。
摘要由CSDN通过智能技术生成

1.写在前面

上篇文章已经介绍过了ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,并对识别二维码进行了详细讲解,ZXing库不但可以识别。同样可以生成条码,而生成条码是一个经常需要使用的功能。
上一篇文章链接如下:
CSDNCSDNicon-default.png?t=N7T8https://mp.csdn.net/mp_blog/creation/editor/137614176

2.项目环境和界面搭建

项目框架:.NET Framework 4.6.1
项目依赖:ZXing.Net    在.NET Core项目中添加ZXing.NET库的引用。可以通过NuGet包管理器或手动下载并添加引用

项目界面设计

3.实现核心代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing;
using ZXing.QrCode;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_Set_Click(object sender, EventArgs e)
        {
            string txt = textBox1.Text.Trim();//获取输入要转换成二维码的信息

            BarcodeFormat format = BarcodeFormat.DATA_MATRIX;//更改format参数可以生成不同类型的条码

            int width = 300;//设置二维码宽度和高度

            int height = 300;

            string filePath = "barcode.png";

            GenerateBarcode(txt, format, width, height, filePath);

            pictureBox1.Image = new Bitmap(filePath);//显示二维码

 
        }

        public static void GenerateBarcode(string text, BarcodeFormat format, int width, int height, string filePath)
        {
            var writer = new BarcodeWriter
            {
                Format = format,
                Options = new QrCodeEncodingOptions
                {
                    Width = width,
                    Height = height
                }
            };
            var barcodeBitmap = writer.Write(text);
            barcodeBitmap.Save(filePath);
        }
    }
}

5.运行效果

如果你觉得我的分享有价值,那就请为我点赞收藏吧!你们的支持是我继续分享的动力。希望大家能够积极评论,让我知道你们的想法和建议。谢谢!
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值