See Also
Other Platforms
Features
Buy Now
Online Help
Demo
Licensing

Back

How to create barcodes using C# or VB.NET and Barcode Professional for ASP.NET that must fit a given size or area

Technologies used
  • Neodynamic Barcode Professional 3.0 for ASP.NET
  • Microsoft .NET Framework (all versions)
It's very common to need to create a barcode image in order it fits a given area, for instance: The barcode must fit an area of size 1.5 inch x 1 inch.
One version of the GetBarcodeImage() method that Barcode Professional features, lets you to accomplish that by specifying the target area through barsAreaSizeInInches parameter. Please keep in mind the following points about the target area:
  • The target area's size (width and height) is measured in inches
  • The target Width includes the whole barcode bars width plus left and right Quiet Zones (QuietZoneWidth property)
  • The target Height includes the barcode bar's height only
Example
You want to encode the data 1234567890 in Code 128 at 300DPI and the barcode must fit an area of size 1 inch x 0.5 inch
Visual Basic .NET
Private Sub SaveBarcode()
    'Create a Barcode Professional object

    Dim bcp As New Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional()
    'Set the barcode symbology to Code 128

    bcp.Symbology = Neodynamic.WebControls.BarcodeProfessional.Symbology.Code128
    'Set the value to encode

    bcp.Code = "1234567890"
    'Barcode dimensions settings

    bcp.BarHeight = 1.0F
    bcp.BarWidth = 0.01F
    'Resolution

    Dim dpi As Single = 300.0F
    'Target size in inches

    Dim targetArea As New System.Drawing.SizeF(1.0F, 0.5F)
    'Get the barcode image fitting the target area

    Dim imgBarcode As System.Drawing.Image = bcp.GetBarcodeImage(dpi, targetArea)
    'Save it on disk in PNG format

    imgBarcode.Save("C:\temp\barcode128.png", System.Drawing.Imaging.ImageFormat.Png)
    imgBarcode.Dispose()
End Sub
Visual C# .NET
private void SaveBarcode()
{
    //Create a Barcode Professional object
    Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional bcp = new Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional();
    //Set the barcode symbology to Code 128
    bcp.Symbology = Neodynamic.WebControls.BarcodeProfessional.Symbology.Code128;
    //Set the value to encode
    bcp.Code = "1234567890";
    //Barcode dimensions settings
    bcp.BarHeight = 1.0f;
    bcp.BarWidth = 0.01f;
    //Resolution
    float dpi = 300.0f;
    //Target size in inches
    System.Drawing.SizeF targetArea = new System.Drawing.SizeF(1.0f, 0.5f);
    //Get the barcode image fitting the target area
    System.Drawing.Image imgBarcode = bcp.GetBarcodeImage(dpi, targetArea);
    //Save it on disk in PNG format
    imgBarcode.Save(@"C:\temp\barcode128.png", System.Drawing.Imaging.ImageFormat.Png);
    imgBarcode.Dispose();
}
Remember that the target Width includes the whole barcode bars width plus left and right Quiet Zones (QuietZoneWidth property) while the target Height includes the barcode bar's height only as is shown in the following figure (produced with the previous code)
If you want to fulfill the whole target area with the barcode only, then you should just set the QuietZoneWidth property to zero (in order to rid of the left and right margins) and set the DisplayCode property to false (in order to hide the human readable text) as is shown in the following code.
Visual Basic .NET
Private Sub SaveBarcode()
    'Create a Barcode Professional object

    Dim bcp As New Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional()
    'Set the barcode symbology to Code 128

    bcp.Symbology = Neodynamic.WebControls.BarcodeProfessional.Symbology.Code128
    'Set the value to encode

    bcp.Code = "1234567890"
    'Barcode dimensions settings

    bcp.BarHeight = 1.0F
    bcp.BarWidth = 0.01F
    bcp.QuietZoneWidth = 0
    bcp.DisplayCode = False
    'Resolution

    Dim dpi As Single = 300.0F
    'Target size in inches

    Dim targetArea As New System.Drawing.SizeF(1.0F, 0.5F)
    'Get the barcode image fitting the target area

    Dim imgBarcode As System.Drawing.Image = bcp.GetBarcodeImage(dpi, targetArea)
    'Save it on disk in PNG format

    imgBarcode.Save("C:\temp\barcode128_full.png", System.Drawing.Imaging.ImageFormat.Png)
    imgBarcode.Dispose()
End Sub
Visual C# .NET
private void SaveBarcode()
{
    //Create a Barcode Professional object
    Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional bcp = new Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional();
    //Set the barcode symbology to Code 128
    bcp.Symbology = Neodynamic.WebControls.BarcodeProfessional.Symbology.Code128;
    //Set the value to encode
    bcp.Code = "1234567890";
    //Barcode dimensions settings
    bcp.BarHeight = 1.0f;
    bcp.BarWidth = 0.01f;
    bcp.QuietZoneWidth = 0;
    bcp.DisplayCode = false;
    //Resolution
    float dpi = 300.0f;
    //Target size in inches
    SizeF targetArea = new System.Drawing.SizeF(1.0f, 0.5f);
    //Get the barcode image fitting the target area
    System.Drawing.Image imgBarcode = bcp.GetBarcodeImage(dpi, targetArea);
    //Save it on disk in PNG format
    imgBarcode.Save(@"C:\temp\barcode128_full.png", System.Drawing.Imaging.ImageFormat.Png);
    imgBarcode.Dispose();
} 
This code will produce the following barcode image. Note that in this case, the barcode fulfills the specified target area.
If you need more information or assistance, please contact our .
 Copyright © 2003 - 2012 Neodynamic S.R.L. All rights reserved.