Как создать изображения штрих-кода из строки в разных форматах с помощью C # с помощью библиотеки barcodelib в WinForms

Если вы работаете с C # и вам нужно сгенерировать одномерные штрих-коды, вы можете знать, что существует не так много библиотек с открытым исходным кодом для рендеринга штрих-кода, поэтому barcodelib является одной из самых известных библиотек для достижения этой цели в C #, это Библиотека, созданная Брэдом Барнхиллом, была выпущена под лицензией Apache, поэтому она будет соответствовать большинству юридических требований многих проектов.

В этом уроке мы покажем вам, как генерировать различные типы штрих-кодов с помощью библиотеки barcodelib в C #.

1. Установите Barcodelib

Чтобы установить этот пакет в свой проект в Visual Studio, перейдите в обозреватель решений и щелкните правой кнопкой мыши свой проект. В раскрывающемся списке выберите параметр «Управление пакетами NuGet»:

Менеджер пакетов NuGet проекта Visual Studio

В диспетчере перейдите на вкладку просмотра и найдите пакет barcodelib:

Библиотека штрихкодов C # WinForms

Выберите первый пакет от Брэда Барнхилла и установите его в свой проект. После завершения установки вы сможете использовать библиотеку для создания изображений штрих-кода в вашем коде. Для получения дополнительной информации об этой библиотеке, пожалуйста, посетите официальный репозиторий на 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);

Код 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 #

ISBN

// 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 #

Ссылка на основную публикацию
Adblock
detector