BarcodeVectorStructure Class
In This Topic
Stores the vector structure of a barcode.
Object Model
Syntax
Example
This C#/VB.NET code shows how to generate barcode structure in a vector form and draw barcode structure on System.Drawing.Bitmap:
''' <summary>
''' Tests the method GenerateBarcodeStructureAndDrawOnBitmap.
''' </summary>
Public Shared Sub Test(outFilename As String)
Using bitmap As System.Drawing.Bitmap = GenerateBarcodeVectorStructureAndDrawOnBitmap(Vintasoft.Barcode.BarcodeType.Code128, "123456789012")
bitmap.Save(outFilename)
End Using
End Sub
''' <summary>
''' Generates the barcode vector structure and draws barcode structure on System.Drawing.Bitmap.
''' </summary>
''' <param name="barcodeType">The barcode type.</param>
''' <param name="barcodeValue">The barcode value.</param>
Public Shared Function GenerateBarcodeVectorStructureAndDrawOnBitmap(barcodeType As Vintasoft.Barcode.BarcodeType, value As String) As System.Drawing.Bitmap
Vintasoft.Barcode.GdiAssembly.Init()
' create the barcode generator
Using writer As New Vintasoft.Barcode.BarcodeWriter()
' specify the barcode type and value
writer.Settings.Barcode = barcodeType
writer.Settings.Value = barcodeValue
' get the vector structure of barcode
Dim barcodeVectorStructure As Vintasoft.Barcode.BarcodeStructure.BarcodeVectorStructure = writer.GetBarcodeVectorStructure()
' get the bounding box of barcode
Dim barcodeBBox As Vintasoft.Primitives.VintasoftRectI = barcodeVectorStructure.GetBoundingBox()
' create Bitmap object that will contain barcode image
Dim result As New System.Drawing.Bitmap(barcodeBBox.Width, barcodeBBox.Height)
' open Graphics object on bitmap
Using graphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(result)
' clear bitmap
graphics.Clear(System.Drawing.Color.White)
' draw the barcode vector structure on System.Drawing.Graphics
DrawBarcodeVectorStructure(barcodeVectorStructure, System.Drawing.Color.Black, graphics)
End Using
Return result
End Using
End Function
''' <summary>
''' Draws the barcode vector structure on System.Drawing.Graphics.
''' </summary>
''' <param name="barcodeVectorStructure">The barcode vector structure.</param>
''' <param name="color">The barcode color.</param>
''' <param name="graphics">The graphics.</param>
Private Shared Sub DrawBarcodeVectorStructure(barcodeVectorStructure As Vintasoft.Barcode.BarcodeStructure.BarcodeVectorStructure, color As System.Drawing.Color, graphics As System.Drawing.Graphics)
Using fillBrush As System.Drawing.Brush = New System.Drawing.SolidBrush(color)
For Each vectorElement As Vintasoft.Barcode.BarcodeStructure.BarcodeVectorElement In barcodeVectorStructure.GetVectorElements()
Select Case vectorElement.ElementType
Case Vintasoft.Barcode.BarcodeStructure.BarcodeVectorElementType.Rectangle
graphics.FillRectangle(fillBrush, Convert(vectorElement.Rect))
Exit Select
Case Vintasoft.Barcode.BarcodeStructure.BarcodeVectorElementType.Text
Dim textElement As Vintasoft.Barcode.BarcodeStructure.BarcodeTextElement = DirectCast(vectorElement, Vintasoft.Barcode.BarcodeStructure.BarcodeTextElement)
Using font As System.Drawing.Font = CreateFont(textElement.FontInfo)
Dim stringFormat As System.Drawing.StringFormat = CreateStringFormat(textElement.LayoutProperties)
graphics.DrawString(textElement.Text, font, fillBrush, Convert(vectorElement.Rect), stringFormat)
End Using
Exit Select
Case Else
Throw New System.NotImplementedException()
End Select
Next
End Using
End Sub
''' <summary>
''' Creates the string format from text layout properties.
''' </summary>
''' <param name="layoutProperties">The layout properties.</param>
Private Shared Function CreateStringFormat(layoutProperties As Vintasoft.Barcode.TextRendering.TextLayoutProperties) As System.Drawing.StringFormat
Dim stringFormat As New System.Drawing.StringFormat()
stringFormat.FormatFlags = stringFormat.FormatFlags Or System.Drawing.StringFormatFlags.NoClip
If layoutProperties.TextAlignment = Vintasoft.Barcode.TextRendering.TextAnchorType.Center Then
stringFormat.Alignment = System.Drawing.StringAlignment.Center
Else
stringFormat.Alignment = System.Drawing.StringAlignment.Near
End If
Return stringFormat
End Function
''' <summary>
''' Creates the font by font information.
''' </summary>
''' <param name="fontInfo">The font information.</param>
Private Shared Function CreateFont(fontInfo As Vintasoft.Barcode.TextRendering.TextFontInfo) As System.Drawing.Font
Dim fontStyle As System.Drawing.FontStyle = System.Drawing.FontStyle.Regular
If fontInfo.IsItalic Then
fontStyle = fontStyle Or System.Drawing.FontStyle.Italic
End If
If fontInfo.IsBold Then
fontStyle = fontStyle Or System.Drawing.FontStyle.Bold
End If
Return New System.Drawing.Font(fontInfo.Name, fontInfo.Size, fontStyle, System.Drawing.GraphicsUnit.Point)
End Function
''' <summary>
''' Converts the specified Vintasoft.Primitives.VintasoftRect to the System.Drawing.RectangleF.
''' </summary>
''' <param name="rect">The rect.</param>
Private Shared Function Convert(rect As Vintasoft.Primitives.VintasoftRect) As System.Drawing.RectangleF
Return New System.Drawing.RectangleF(CSng(rect.X), CSng(rect.Y), CSng(rect.Width), CSng(rect.Height))
End Function
/// <summary>
/// Tests the method GenerateBarcodeStructureAndDrawOnBitmap.
/// </summary>
public static void Test(string outFilename)
{
using (System.Drawing.Bitmap bitmap = GenerateBarcodeVectorStructureAndDrawOnBitmap(Vintasoft.Barcode.BarcodeType.Code128, "123456789012"))
{
bitmap.Save(outFilename);
}
}
/// <summary>
/// Generates the barcode vector structure and draws barcode structure on System.Drawing.Bitmap.
/// </summary>
/// <param name="barcodeType">The barcode type.</param>
/// <param name="barcodeValue">The barcode value.</param>
public static System.Drawing.Bitmap GenerateBarcodeVectorStructureAndDrawOnBitmap(
Vintasoft.Barcode.BarcodeType barcodeType,
string barcodeValue)
{
Vintasoft.Barcode.GdiAssembly.Init();
// create the barcode generator
using (Vintasoft.Barcode.BarcodeWriter writer = new Vintasoft.Barcode.BarcodeWriter())
{
// specify the barcode type and value
writer.Settings.Barcode = barcodeType;
writer.Settings.Value = barcodeValue;
// get the vector structure of barcode
Vintasoft.Barcode.BarcodeStructure.BarcodeVectorStructure barcodeVectorStructure = writer.GetBarcodeVectorStructure();
// get the bounding box of barcode
Vintasoft.Primitives.VintasoftRectI barcodeBBox = barcodeVectorStructure.GetBoundingBox();
// create Bitmap object that will contain barcode image
System.Drawing.Bitmap result = new System.Drawing.Bitmap(barcodeBBox.Width, barcodeBBox.Height);
// open Graphics object on bitmap
using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(result))
{
// clear bitmap
graphics.Clear(System.Drawing.Color.White);
// draw the barcode vector structure on System.Drawing.Graphics
DrawBarcodeVectorStructure(barcodeVectorStructure, System.Drawing.Color.Black, graphics);
}
return result;
}
}
/// <summary>
/// Draws the barcode vector structure on System.Drawing.Graphics.
/// </summary>
/// <param name="barcodeVectorStructure">The barcode vector structure.</param>
/// <param name="color">The barcode color.</param>
/// <param name="graphics">The graphics.</param>
private static void DrawBarcodeVectorStructure(
Vintasoft.Barcode.BarcodeStructure.BarcodeVectorStructure barcodeVectorStructure,
System.Drawing.Color color,
System.Drawing.Graphics graphics)
{
using (System.Drawing.Brush fillBrush = new System.Drawing.SolidBrush(color))
{
foreach (Vintasoft.Barcode.BarcodeStructure.BarcodeVectorElement vectorElement in barcodeVectorStructure.GetVectorElements())
{
switch (vectorElement.ElementType)
{
case Vintasoft.Barcode.BarcodeStructure.BarcodeVectorElementType.Rectangle:
graphics.FillRectangle(fillBrush, Convert(vectorElement.Rect));
break;
case Vintasoft.Barcode.BarcodeStructure.BarcodeVectorElementType.Text:
Vintasoft.Barcode.BarcodeStructure.BarcodeTextElement textElement =
(Vintasoft.Barcode.BarcodeStructure.BarcodeTextElement)vectorElement;
using (System.Drawing.Font font = CreateFont(textElement.FontInfo))
{
System.Drawing.StringFormat stringFormat = CreateStringFormat(textElement.LayoutProperties);
graphics.DrawString(textElement.Text, font, fillBrush, Convert(vectorElement.Rect), stringFormat);
}
break;
default:
throw new System.NotImplementedException();
}
}
}
}
/// <summary>
/// Creates the string format from text layout properties.
/// </summary>
/// <param name="layoutProperties">The layout properties.</param>
private static System.Drawing.StringFormat CreateStringFormat(Vintasoft.Barcode.TextRendering.TextLayoutProperties layoutProperties)
{
System.Drawing.StringFormat stringFormat = new System.Drawing.StringFormat();
stringFormat.FormatFlags |= System.Drawing.StringFormatFlags.NoClip;
if (layoutProperties.TextAlignment == Vintasoft.Barcode.TextRendering.TextAnchorType.Center)
stringFormat.Alignment = System.Drawing.StringAlignment.Center;
else
stringFormat.Alignment = System.Drawing.StringAlignment.Near;
return stringFormat;
}
/// <summary>
/// Creates the font by font information.
/// </summary>
/// <param name="fontInfo">The font information.</param>
private static System.Drawing.Font CreateFont(Vintasoft.Barcode.TextRendering.TextFontInfo fontInfo)
{
System.Drawing.FontStyle fontStyle = System.Drawing.FontStyle.Regular;
if (fontInfo.IsItalic)
fontStyle |= System.Drawing.FontStyle.Italic;
if (fontInfo.IsBold)
fontStyle |= System.Drawing.FontStyle.Bold;
return new System.Drawing.Font(fontInfo.Name, fontInfo.Size, fontStyle, System.Drawing.GraphicsUnit.Point);
}
/// <summary>
/// Converts the specified Vintasoft.Primitives.VintasoftRect to the System.Drawing.RectangleF.
/// </summary>
/// <param name="rect">The rect.</param>
private static System.Drawing.RectangleF Convert(Vintasoft.Primitives.VintasoftRect rect)
{
return new System.Drawing.RectangleF(
(float)rect.X,
(float)rect.Y,
(float)rect.Width,
(float)rect.Height
);
}
Inheritance Hierarchy
Requirements
Target Platforms: .NET9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5
See Also