本文概述
- 1.安装Barcodelib
- 2.生成条形码图像
在本教程中, 我们将向你展示如何使用C#中的条码库生成不同类型的条码。
1.安装Barcodelib 要在Visual Studio中的项目上安装此软件包, 请转到解决方案资源管理器, 然后右键单击你的项目。从下拉列表中选择管理NuGet软件包选项:
文章图片
在管理器中, 转到浏览选项卡并搜索条形码库包:
文章图片
【如何在WinForms中使用Barcodelib库使用C#从具有不同格式的字符串中创建条形码图像】选择Brad Barnhill的第一个软件包并将其安装在你的项目中。安装完成后, 你将可以使用该库在代码上创建条形码图像。有关此库的更多信息, 请访问Github上的官方存储库。在项目中安装库之后, 你将能够导入条形码库名称空间和.NET的图像类:
// Barcodelib namespaceusing BarcodeLib;
// .net required namespacesusing System.Drawing;
using System.Drawing.Imaging;
using Color = System.Drawing.Color;
2.生成条形码图像 理解如何使用C#中的此库生成条形码的最佳方法基本上是通过示例:
UPC-A
// Create an instance of the APIBarcode barcodeAPI = new Barcode();
// Define basic settings of the imageint imageWidth = 290;
int imageHeight = 120;
Color foreColor = Color.Black;
Color backColor = Color.Transparent;
string data = "http://www.srcmini.com/038000356216";
// Generate the barcode with your settingsImage barcodeImage = barcodeAPI.Encode(TYPE.UPCA, data, foreColor, backColor, imageWidth, imageHeight);
// Store image in some path with the desired formatbarcodeImage.Save(@"C:\Users\sdkca\Desktop\upca_example.png", ImageFormat.Png);
文章图片
代码128
// Create an instance of the APIBarcode barcodeAPI = new Barcode();
// Define basic settings of the imageint imageWidth = 290;
int imageHeight = 120;
Color foreColor = Color.Black;
Color backColor = Color.Transparent;
string data = "http://www.srcmini.com/ABC-abc-1234";
// Generate the barcode with your settingsImage barcodeImage = barcodeAPI.Encode(TYPE.CODE128, data, foreColor, backColor, imageWidth, imageHeight);
// Store image in some path with the desired formatbarcodeImage.Save(@"C:\Users\sdkca\Desktop\code128_example.png", ImageFormat.Png);
文章图片
代码11
// Create an instance of the APIBarcode barcodeAPI = new Barcode();
// Define basic settings of the imageint imageWidth = 290;
int imageHeight = 120;
Color foreColor = Color.Black;
Color backColor = Color.Transparent;
string data = "http://www.srcmini.com/0123-4567";
// Generate the barcode with your settingsImage barcodeImage = barcodeAPI.Encode(TYPE.CODE11, data, foreColor, backColor, imageWidth, imageHeight);
// Store image in some path with the desired formatbarcodeImage.Save(@"C:\Users\sdkca\Desktop\code11_example.png", ImageFormat.Png);
文章图片
书号
// Create an instance of the APIBarcode barcodeAPI = new Barcode();
// Define basic settings of the imageint imageWidth = 290;
int imageHeight = 120;
Color foreColor = Color.Black;
Color backColor = Color.Transparent;
string data = "http://www.srcmini.com/9781234567897";
// Generate the barcode with your settingsImage barcodeImage = barcodeAPI.Encode(TYPE.ISBN, data, foreColor, backColor, imageWidth, imageHeight);
// Store image in some path with the desired formatbarcodeImage.Save(@"C:\Users\sdkca\Desktop\isbn_example.png", ImageFormat.Png);
文章图片
ITF14
// Create an instance of the APIBarcode barcodeAPI = new Barcode();
// Define basic settings of the imageint imageWidth = 290;
int imageHeight = 120;
Color foreColor = Color.Black;
Color backColor = Color.Transparent;
string data = "http://www.srcmini.com/17350053850252";
// Generate the barcode with your settingsImage barcodeImage = barcodeAPI.Encode(TYPE.ITF14, data, foreColor, backColor, imageWidth, imageHeight);
// Store image in some path with the desired formatbarcodeImage.Save(@"C:\Users\sdkca\Desktop\itf14_example.png", ImageFormat.Png);
文章图片
EAN13
// Create an instance of the APIBarcode barcodeAPI = new Barcode();
// Define basic settings of the imageint imageWidth = 290;
int imageHeight = 120;
Color foreColor = Color.Black;
Color backColor = Color.Transparent;
string data = "http://www.srcmini.com/978020137962";
// Generate the barcode with your settingsImage barcodeImage = barcodeAPI.Encode(TYPE.EAN13, data, foreColor, backColor, imageWidth, imageHeight);
// Store image in some path with the desired formatbarcodeImage.Save(@"C:\Users\sdkca\Desktop\ean13_example.png", ImageFormat.Png);
文章图片
编码愉快!
推荐阅读
- 如何在Symfony 3中合并多个PDF
- Winforms跨线程操作无效(从不是在其上创建线程的线程访问的控件”控件名”)
- 如何在Windows中使用Swift编程语言
- 每个Twig开发人员都应该能够回答的20个问题
- 如何使用WinForms中的OpenCVSharp库和带有C#的网络摄像机拍摄快照
- 如何在C#中使用AES加密算法对文件进行加密和解密
- 如何使用Visual Studio Code的便携式版本
- 缺少来自android appcompat v7-21.0.0的样式
- 如何使用像Android Studio这样的pid杀死进程呢()