记录【WinForm】C#学习使用ZXing.Net生成条码过程

本文详细介绍了如何在C#的WinForm应用中使用ZXing.Net库来生成、保存二维码和条形码,并实现图像拖放解析内容。步骤包括创建用户界面、安装库、设置控件、处理事件,以及处理可能出现的问题。最后展示了生成和识别的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录一次学习过程,参考来源于【WinForm】C#使用ZXing.Net生成二维码和条形码,包含识别条码内容_GreAmbWang的博客-优快云博客_c# zxing decodeimage,感谢你的分享!

第一步做一个窗体界面

分别有的控件名称:1)cmbBarcodeFormat;2)tbWidth;3)tbHeight;4)tbContent;5)btnGenerate;6)btnSave;7)picBarcode;

第二步在管理NuGet程序包下载ZXing.net开源工具包

第三步添加引用

using ZXing;
using ZXing.Presentation;
using ZXing.QrCode;
using BarcodeReader = ZXing.BarcodeReader;
using BarcodeWriter = ZXing.BarcodeWriter;

第四步实现窗口初始化载入comboBox的选项数据

        private void Form1_Load(object sender, EventArgs e)
        {
            Array array = Enum.GetValues(typeof(BarcodeFormat));

            Dictionary<BarcodeFormat, string> bFs = new Dictionary<BarcodeFormat, string>();

            foreach (var item in array)
            {
                bFs.Add((BarcodeFormat)item, item.ToString());
            }

            cmbBarcodeFormat.DataSource = new BindingSource() { DataSource = bFs };

            cmbBarcodeFormat.ValueMember = "Key";

            cmbBarcodeFormat.DisplayMember = "Value";

        }

第五步实现按钮事件生成条码

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            if (!int.TryParse(tbWidth.Text, out int width))
            {
                MessageBox.Show("请输入正确整数", "提示");
                return;
            }
            if (!int.TryParse(tbHeight.Text, out int height))
            {
                MessageBox.Show("请输入正确整数", "提示");
                return;
            }
            if (tbContent.Text == string.Empty)
            {
                MessageBox.Show("请输入内容", "提示");
                return;
            }

            picBarcode.Width = width;
            picBarcode.Height = height;

            BarcodeWriter barcodeWriter = new BarcodeWriter()
            {
                Format = (BarcodeFormat)cmbBarcodeFormat.SelectedValue ,

                Options = new QrCodeEncodingOptions
                {
                    DisableECI = true,
                    CharacterSet = "UTF-8",
                    Width = width,
                    Height = height,
                    Margin = 0
                }
            };

                try
                {
                    picBarcode.Image = barcodeWriter.Write(tbContent.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("生成失败\r\n" + ex.Message, "提示");
                }
        
        }

第六步实现条码的保存

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Control control = picBarcode;
                Bitmap bitmap = new Bitmap(control.Width, control.Height);
                control.DrawToBitmap(bitmap, new Rectangle(0, 0, control.Width, control.Height));

                SaveFileDialog saveFileDialog = new SaveFileDialog()
                {
                    Filter = "图片(*.png)|*.png|图片(*.jpg)|*.jpg|所有文件|*.*",
                    FileName = "barcode",
                    RestoreDirectory = true
                };

                if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
                {
                    string fileName = saveFileDialog.FileName;
                    bitmap.Save(fileName);

                    string path = fileName.Substring(0, fileName.LastIndexOf("\\"));
                    System.Diagnostics.Process.Start(path);//打开文件夹
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败\r\n" + ex.Message, "提示");
            }
        }

第七步修改窗体AllowDrop属性为True

第九步建立DragDrop(FrmBarcode_DragDrop)拖放完成时发生事件

和DragEnter(FrmBarcode_DragEnter)在用鼠标将某项拖动到该控件的工作区时发生事件

BarcodeReader barcodeReader = new BarcodeReader();

        private void FrmBarcode_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Link : DragDropEffects.None;
        }

        private void FrmBarcode_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
                picBarcode.Load(path);

                Result result = barcodeReader.Decode((Bitmap)picBarcode.Image);
                tbContent.Text = result?.Text;
            }
            catch (Exception ex)
            {
                MessageBox.Show("解析失败\r\n" + ex.Message, "提示");
            }
        }

经过以上步骤就可以实现条码的生成、保存与解析了,虽然也是会出现某些条码类型不能正确生成的现象,如果有知道原因的同学可以告诉一下我咯;

下面展示一下生成效果:

再次感谢【WinForm】C#使用ZXing.Net生成二维码和条形码,包含识别条码内容_GreAmbWang的博客-优快云博客_c# zxing decodeimage原文作者的分享,谢谢你的分享!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值