В этом разделе
Класс, который позволяет генерировать штрих-коды в виде растрового изображения, графического пути, файла SVG или структуры штрих-кода.
Объектная модель
Синтаксис
'Declaration
Public NotInheritable Class BarcodeWriter
public sealed class BarcodeWriter
public __gc __sealed class BarcodeWriter
public ref class BarcodeWriter sealed
Пример
Этот C#/VB. NET показывает, как создать изображение со штрих-кодом Code128.
Imports Vintasoft.Barcode
Imports Vintasoft.Barcode.SymbologySubsets
Imports Vintasoft.Imaging
''' <summary>
''' Test that shows how to create a barcode image using the BarcodeWriter class.
''' </summary>
Class BarcodeWriterExample
''' <summary>
''' Generates Code128 barcode with specified size, in pixels.
''' </summary>
''' <param name="value">The value.</param>
''' <param name="width">The image width, in pixels.</param>
''' <param name="height">The image height, in pixels.</param>
Public Shared Sub TestCode128Barcode(value As String, width As Integer, height As Integer)
' create the barcode writer
Using writer As New BarcodeWriter()
' set barcode writer settings
writer.Settings.Barcode = BarcodeType.Code128
writer.Settings.Value = value
writer.Settings.Resolution = 96
'DPI
writer.Settings.PixelFormat = BarcodeImagePixelFormat.Bgr24
' create image with barcode
Using barcodeImage As VintasoftBitmap = writer.GetBarcodeAsVintasoftBitmap(width, height, Vintasoft.Barcode.UnitOfMeasure.Pixels)
' save barcode image to a file
ImageCodecs.[Default].Encode(barcodeImage, String.Format("barcode_code128_{0}x{1}.png", width, height))
End Using
End Using
End Sub
''' <summary>
''' Generates Code128 barcode.
''' </summary>
Public Shared Sub TestCode128Barcode()
' create the barcode writer
Using writer As New BarcodeWriter()
' specify that writer must generate Code128 barcode
writer.Settings.Barcode = BarcodeType.Code128
' specify that writer must create output image as 24-bpp image
writer.Settings.PixelFormat = BarcodeImagePixelFormat.Bgr24
' specify that single bar width must be 3 pixels
writer.Settings.MinWidth = 3
' set the barcode value
writer.Settings.Value = "Test1234567"
' save the barcode image to a file
writer.SaveBarcodeAsImage("barcode_code128.png")
End Using
End Sub
''' <summary>
''' Generates SSCC-18 barcode.
''' </summary>
Public Shared Sub TestSSCC18Barcode()
' create the barcode writer
Using writer As New BarcodeWriter()
' specify that writer must create output image as 24-bpp image
writer.Settings.PixelFormat = BarcodeImagePixelFormat.Bgr24
' specify that single bar width must be 3 pixels
writer.Settings.MinWidth = 3
' encode SSCC-18 barcode
BarcodeSymbologySubsets.SSCC18.Encode("012345678901234560", writer.Settings)
' save the barcode image to a file
writer.SaveBarcodeAsImage("barcode_SSCC18.png")
End Using
End Sub
''' <summary>
''' Generates GS1-128 barcode.
''' </summary>
Public Shared Sub TestGS1_128Barcode()
' create the barcode writer
Using writer As New BarcodeWriter()
' specify that writer must create output image as 24-bpp image
writer.Settings.PixelFormat = BarcodeImagePixelFormat.Bgr24
' specify that single bar width must be 3 pixels
writer.Settings.MinWidth = 3
' encode GS1-128 barcode
' GTIN : 08699736690061
' SERIAL : 30000000000720
' USE BY OR EXPIRY : 110227
' BATCH/LOT : TEST
BarcodeSymbologySubsets.GS1_128.Encode("(01)08699736690061(21)30000000000720(17)110227(10)TEST", writer.Settings)
' save the barcode image to a file
writer.SaveBarcodeAsImage("barcode_GS1-128.png")
End Using
End Sub
End Class
using Vintasoft.Barcode;
using Vintasoft.Barcode.SymbologySubsets;
using Vintasoft.Imaging;
/// <summary>
/// Test that shows how to create a barcode image using the BarcodeWriter class.
/// </summary>
class BarcodeWriterExample
{
/// <summary>
/// Generates Code128 barcode with specified size, in pixels.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="width">The image width, in pixels.</param>
/// <param name="height">The image height, in pixels.</param>
public static void TestCode128Barcode(string value, int width, int height)
{
// create the barcode writer
using (BarcodeWriter writer = new BarcodeWriter())
{
// set barcode writer settings
writer.Settings.Barcode = BarcodeType.Code128;
writer.Settings.Value = value;
writer.Settings.Resolution = 96;//DPI
writer.Settings.PixelFormat = BarcodeImagePixelFormat.Bgr24;
// create image with barcode
using (VintasoftBitmap barcodeImage = writer.GetBarcodeAsVintasoftBitmap(width, height, Vintasoft.Barcode.UnitOfMeasure.Pixels))
{
// save barcode image to a file
ImageCodecs.Default.Encode(barcodeImage, string.Format("barcode_code128_{0}x{1}.png", width, height));
}
}
}
/// <summary>
/// Generates Code128 barcode.
/// </summary>
public static void TestCode128Barcode()
{
// create the barcode writer
using (BarcodeWriter writer = new BarcodeWriter())
{
// specify that writer must generate Code128 barcode
writer.Settings.Barcode = BarcodeType.Code128;
// specify that writer must create output image as 24-bpp image
writer.Settings.PixelFormat = BarcodeImagePixelFormat.Bgr24;
// specify that single bar width must be 3 pixels
writer.Settings.MinWidth = 3;
// set the barcode value
writer.Settings.Value = "Test1234567";
// save the barcode image to a file
writer.SaveBarcodeAsImage("barcode_code128.png");
}
}
/// <summary>
/// Generates SSCC-18 barcode.
/// </summary>
public static void TestSSCC18Barcode()
{
// create the barcode writer
using (BarcodeWriter writer = new BarcodeWriter())
{
// specify that writer must create output image as 24-bpp image
writer.Settings.PixelFormat = BarcodeImagePixelFormat.Bgr24;
// specify that single bar width must be 3 pixels
writer.Settings.MinWidth = 3;
// encode SSCC-18 barcode
BarcodeSymbologySubsets.SSCC18.Encode("012345678901234560", writer.Settings);
// save the barcode image to a file
writer.SaveBarcodeAsImage("barcode_SSCC18.png");
}
}
/// <summary>
/// Generates GS1-128 barcode.
/// </summary>
public static void TestGS1_128Barcode()
{
// create the barcode writer
using (BarcodeWriter writer = new BarcodeWriter())
{
// specify that writer must create output image as 24-bpp image
writer.Settings.PixelFormat = BarcodeImagePixelFormat.Bgr24;
// specify that single bar width must be 3 pixels
writer.Settings.MinWidth = 3;
// encode GS1-128 barcode
// GTIN : 08699736690061
// SERIAL : 30000000000720
// USE BY OR EXPIRY : 110227
// BATCH/LOT : TEST
BarcodeSymbologySubsets.GS1_128.Encode(
"(01)08699736690061(21)30000000000720(17)110227(10)TEST", writer.Settings);
// save the barcode image to a file
writer.SaveBarcodeAsImage("barcode_GS1-128.png");
}
}
}
Иерархия наследования
System.Object
 Vintasoft.Barcode.BarcodeWriter
Требования
Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5
Смотрите также