Как сгенерировать штрих-код с указанным размером и разрешением?
В этом разделе
Пример: Вот пример, который показывает, как сгенерировать штрих-код с заданным размером и разрешением.
/// <summary>
/// This example shows how to draw a barcode with specified size and resolution.
/// </summary>
public static class DrawBarcodeWithSpecifiedSizeAndResolution
{
/// <summary>
/// Initializes the <see cref="DrawBarcodeWithSpecifiedSizeAndResolution"/> class.
/// </summary>
static DrawBarcodeWithSpecifiedSizeAndResolution()
{
// initialize Vintasoft.Barcode.Gdi assembly - must be called only once
Vintasoft.Barcode.GdiAssembly.Init();
}
/// <summary>
/// Creates a barcode image and saves it to specified path.
/// </summary>
/// <param name="filename">The filename of result barcode image.</param>
public static void TestDrawBarcode(string filename)
{
// draw barcode with size 6 x 2 cm and resolution 300 dpi
using (Vintasoft.Imaging.VintasoftBitmap barcodeImage = DrawBarcode(
Vintasoft.Barcode.BarcodeType.Code128,
"TESTBARCODE",
300,
6,
2,
Vintasoft.Barcode.UnitOfMeasure.Centimeters))
{
// save barcode image to a file
Vintasoft.Barcode.ImageCodecs.Default.Encode(barcodeImage, filename);
}
}
/// <summary>
/// Creates a bitmap with specified barcode.
/// </summary>
/// <param name="barcode">Barcode to draw.</param>
/// <param name="value">Barcode value.</param>
/// <param name="resolution">Bitmap resolution.</param>
/// <param name="width">Bitmap width.</param>
/// <param name="height">Bitmap height.</param>
/// <param name="units">Size units.</param>
public static Vintasoft.Imaging.VintasoftBitmap DrawBarcode(
Vintasoft.Barcode.BarcodeType barcode,
string value,
float resolution,
float width,
float height,
Vintasoft.Barcode.UnitOfMeasure units)
{
// create the barcode writer
using (Vintasoft.Barcode.BarcodeWriter writer = new Vintasoft.Barcode.BarcodeWriter())
{
// set barcode writer settings
writer.Settings.Barcode = barcode;
writer.Settings.Value = value;
writer.Settings.Resolution = resolution;
writer.Settings.PixelFormat = Vintasoft.Barcode.BarcodeImagePixelFormat.Bgr24;
// return barcode image
return writer.GetBarcodeAsVintasoftBitmap(width, height, units);
}
}
}
''' <summary>
''' This example shows how to draw a barcode with specified size and resolution.
''' </summary>
Public NotInheritable Class DrawBarcodeWithSpecifiedSizeAndResolution
Private Sub New()
End Sub
''' <summary>
''' Initializes the <see cref="DrawBarcodeWithSpecifiedSizeAndResolution"/> class.
''' </summary>
Shared Sub New()
' initialize Vintasoft.Barcode.Gdi assembly - must be called only once
Vintasoft.Barcode.GdiAssembly.Init()
End Sub
''' <summary>
''' Creates a barcode image and saves it to specified path.
''' </summary>
''' <param name="filename">The filename of result barcode image.</param>
Public Shared Sub TestDrawBarcode(filename As String)
' draw barcode with size 6 x 2 cm and resolution 300 dpi
Using barcodeImage As Vintasoft.Imaging.VintasoftBitmap = DrawBarcode(Vintasoft.Barcode.BarcodeType.Code128, "TESTBARCODE", 300, 6, 2, Vintasoft.Barcode.UnitOfMeasure.Centimeters)
' save barcode image to a file
Vintasoft.Barcode.ImageCodecs.[Default].Encode(barcodeImage, filename)
End Using
End Sub
''' <summary>
''' Creates a bitmap with specified barcode.
''' </summary>
''' <param name="barcode">Barcode to draw.</param>
''' <param name="value">Barcode value.</param>
''' <param name="resolution">Bitmap resolution.</param>
''' <param name="width">Bitmap width.</param>
''' <param name="height">Bitmap height.</param>
''' <param name="units">Size units.</param>
Public Shared Function DrawBarcode(barcode As Vintasoft.Barcode.BarcodeType, value As String, resolution As Single, width As Single, height As Single, units As Vintasoft.Barcode.UnitOfMeasure) As Vintasoft.Imaging.VintasoftBitmap
' create the barcode writer
Using writer As New Vintasoft.Barcode.BarcodeWriter()
' set barcode writer settings
writer.Settings.Barcode = barcode
writer.Settings.Value = value
writer.Settings.Resolution = resolution
writer.Settings.PixelFormat = Vintasoft.Barcode.BarcodeImagePixelFormat.Bgr24
' return barcode image
Return writer.GetBarcodeAsVintasoftBitmap(width, height, units)
End Using
End Function
End Class