个性化阅读
专注于IT技术分析

如何在WinForms中使用Barcodelib库使用C#从具有不同格式的字符串中创建条形码图像

本文概述

如果你正在使用C#, 并且需要生成一维条形码, 你可能会知道没有很多开源条形码渲染库, 这就是为什么条形码库是在C#中实现这一目标的最著名的库之一, Brad Barnhill创建的库已根据Apache许可发布, 因此它符合许多项目的大多数法律要求。

在本教程中, 我们将向你展示如何使用C#中的条码库生成不同类型的条码。

1.安装Barcodelib

要在Visual Studio中的项目上安装此软件包, 请转到解决方案资源管理器, 然后右键单击你的项目。从下拉列表中选择管理NuGet软件包选项:

NuGet包管理器项目Visual Studio

在管理器中, 转到浏览选项卡并搜索条形码库包:

Barcodelib库C#WinForms

选择Brad Barnhill的第一个软件包并将其安装在你的项目中。安装完成后, 你将可以使用该库在代码上创建条形码图像。有关此库的更多信息, 请访问Github上的官方存储库。在项目中安装库之后, 你将能够导入条形码库名称空间和.NET的图像类:

// Barcodelib namespace
using BarcodeLib;

// .net required namespaces
using System.Drawing;
using System.Drawing.Imaging;
using Color = System.Drawing.Color;

2.生成条形码图像

理解如何使用C#中的此库生成条形码的最佳方法基本上是通过示例:

UPC-A

// Create an instance of the API
Barcode barcodeAPI = new Barcode();

// Define basic settings of the image
int imageWidth = 290;
int imageHeight = 120;
Color foreColor = Color.Black;
Color backColor = Color.Transparent;
string data = "038000356216";

// Generate the barcode with your settings
Image barcodeImage = barcodeAPI.Encode(TYPE.UPCA, data, foreColor, backColor, imageWidth, imageHeight);

// Store image in some path with the desired format
barcodeImage.Save(@"C:\Users\sdkca\Desktop\upca_example.png", ImageFormat.Png);
UPC-A条码C#

代码128

// Create an instance of the API
Barcode barcodeAPI = new Barcode();

// Define basic settings of the image
int imageWidth = 290;
int imageHeight = 120;
Color foreColor = Color.Black;
Color backColor = Color.Transparent;
string data = "ABC-abc-1234";

// Generate the barcode with your settings
Image barcodeImage = barcodeAPI.Encode(TYPE.CODE128, data, foreColor, backColor, imageWidth, imageHeight);

// Store image in some path with the desired format
barcodeImage.Save(@"C:\Users\sdkca\Desktop\code128_example.png", ImageFormat.Png);
Code 128条码C#

代码11

// Create an instance of the API
Barcode barcodeAPI = new Barcode();

// Define basic settings of the image
int imageWidth = 290;
int imageHeight = 120;
Color foreColor = Color.Black;
Color backColor = Color.Transparent;
string data = "0123-4567";

// Generate the barcode with your settings
Image barcodeImage = barcodeAPI.Encode(TYPE.CODE11, data, foreColor, backColor, imageWidth, imageHeight);

// Store image in some path with the desired format
barcodeImage.Save(@"C:\Users\sdkca\Desktop\code11_example.png", ImageFormat.Png);
Code11条码C#

书号

// Create an instance of the API
Barcode barcodeAPI = new Barcode();

// Define basic settings of the image
int imageWidth = 290;
int imageHeight = 120;
Color foreColor = Color.Black;
Color backColor = Color.Transparent;
string data = "9781234567897";

// Generate the barcode with your settings
Image barcodeImage = barcodeAPI.Encode(TYPE.ISBN, data, foreColor, backColor, imageWidth, imageHeight);

// Store image in some path with the desired format
barcodeImage.Save(@"C:\Users\sdkca\Desktop\isbn_example.png", ImageFormat.Png);
ISBN条形码C#示例

ITF14

// Create an instance of the API
Barcode barcodeAPI = new Barcode();

// Define basic settings of the image
int imageWidth = 290;
int imageHeight = 120;
Color foreColor = Color.Black;
Color backColor = Color.Transparent;
string data = "17350053850252";

// Generate the barcode with your settings
Image barcodeImage = barcodeAPI.Encode(TYPE.ITF14, data, foreColor, backColor, imageWidth, imageHeight);

// Store image in some path with the desired format
barcodeImage.Save(@"C:\Users\sdkca\Desktop\itf14_example.png", ImageFormat.Png);
ITF14条码示例

EAN13

// Create an instance of the API
Barcode barcodeAPI = new Barcode();

// Define basic settings of the image
int imageWidth = 290;
int imageHeight = 120;
Color foreColor = Color.Black;
Color backColor = Color.Transparent;
string data = "978020137962";

// Generate the barcode with your settings
Image barcodeImage = barcodeAPI.Encode(TYPE.EAN13, data, foreColor, backColor, imageWidth, imageHeight);

// Store image in some path with the desired format
barcodeImage.Save(@"C:\Users\sdkca\Desktop\ean13_example.png", ImageFormat.Png);
EAN13条码C#

编码愉快!

赞(0)
未经允许不得转载:srcmini » 如何在WinForms中使用Barcodelib库使用C#从具有不同格式的字符串中创建条形码图像

评论 抢沙发

评论前必须登录!