记录一次学习过程,参考来源于【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原文作者的分享,谢谢你的分享!