dLSoft  barcode & labelling software     

Generating barcodes in C#

Shows how to create barcodes in C# projects using either a Component or a WinForms control included in the dBarcode.NET components. The discussion here focuses on using the component or control in Windows Forms projects in Visual Studio, but the components may be used in any .NET or ASP.NET project.

Components are available for all major 1D and 2D barcode types and create high resolution images with full control over all barcode properties.

Barcode component on C# form

Adding a dBarcode.NET product to the Visual Studio ToolBox

To add a dBarcode Component or Control to the Visual Studio ToolBox, display the ToolBox and select the Components tab. Right click on the Components pane and select Add/Remove Items.. from the pop-up menu displayed. A dialog box is displayed listing the currently installed components. Ensure that the .NET Framework Components page is displayed.

Push the Browse button and navigate to the location where you have installed or copied your dBarcode.NET component and select the DLL (e.g. AbcnetLib.dll for the Component or dBarcode1D.dll for the Control).

Then push the Open button.

The list of installed components is now displayed, including your dBarcode.NET component or control. Ensure that the checkbox alongside the component name is checked. Now push the OK button.

Using the control to generate barcodes in C#

Adding a dBarcode.NET Control to a Windows Form

With a project's form open in design mode drag the dBarcode.NET control icon from the toolbox onto the form.

The control appears on the form using default settings. The control may be positioned by dragging around the form, and multiple controls may appear on a single form.  The first to be added will be called Barcc1d11, the second Barcc1d2, and so on; the names may be changed by the user by modifying the Name property within the Properties box.

dBarcode.Net control    dBarcode.Net design-time dialog
Design-time menu                                 Design time Properties dialog

The default properties for the control may be set by selecting the control, right-clicking on it and selecting "Show barcode properties dialog" from the pop-up menu displayed. The values you provide here will be used as defaults unless properties are changed programmatically within your project.

The control is always displayed on the form (unless you turn its Visible property to false), so no additional code is required. The barcode content can now be changed programmatically by changing the Caption property, eg.


The Barcode properties dialog may also be displayed at run-tim by calling the dBarcode1.doProps() function, and the barcode may be saved or copied to the clipboard with a specified graphics resolution using the following functions:



where the filename extension (.BMP, .EPS, .GIF, .JPG, .PNG, .TIF, .WMF) specifies the graphics type.

Using the Component to generate barcodes in C#

If you prefer to use a component rather than the control to create barcodes in C# projects ....

Adding a dBarcode.NET component to a C# project.

With a project's form open in design mode drag the dBarcode.NET component icon from the toolbox onto the form.

The component icon appears on the panel below the form – it does NOT appear on the form itself. The instance of the component will be given a default name (eg Abcnet1) which appears in the properties panel when the component is selected. A single Form may contain any number of dBarcode.NET Components. The first to be added will be called Abcnet1, the second Abcnet2, and so on; the names may be changed by the user by modifying the Name property within the Properties box.

The properties panel also displays all other settable properties for the component, and these values will be used as defaults unless properties are changed programmatically within your project.

Setting and retrieving property values programmatically

The dBarcode.NET Components may be operated entirely by setting or retrieving Property values programmatically.

Clicking on the dBarcode Component in the panel under the form when Visual Studio's Properties box is displayed will show the current settings for component's available properties. Most of these may be edited using the Properties box, or may have their values set from within the user's program by statements of the kind

        Abcnet1.Caption="1234";             C#

dBarcode Component properties that are set AFTER a barcode has been created may be retrieved within user's programs by statements of the kind:

        x=Abcnet1.Error;         C#

Setting properties through the Barcode properties dialog box

Using the Method AbcProperties() causes the Barcode properties dialog to be displayed at run-time. This displays all settable properties in a convenient form and enables changes to be made by selecting from drop-down lists or entering values into edit boxes, or summoning standard Windows dialogs for the selection of colors or fonts.

 Setting barcode properties in a Visual Studio.NET application

Displaying a barcode on a form

To display a barcode on a form a PictureBox is used to hold the image.

Place a PictureBox Windows Forms control on the form and add some code to your program to take the barcode image from the dBarcode.NET component and use it as the Image for the PictureBox

For example:

private void DoBarcode()                  C#
      Graphics g=pictureBox1.CreateGraphics();

Printing a barcode image

Printing the image returned by the Barcode() call may be accomplished by any of the printing techniques available for Visual Studio.NET project. However, probably the most useful approach is to use the DrawImage() method in a PrintPage handler as illustrated below, and in the examples provided with the components:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
      e.Graphics.PageUnit = GraphicsUnit.Document;
      e.Graphics.DrawImage(abcnet1.Barcode(e.Graphics), 500F,500F);
      e.HasMorePages = false;

While the PageUnit  setting can be any of the allowed values, we have found that the most accurately sized barcodes are produced when the highest resolution setting (Document, equivalent to 300 units per inch) is used.

Saving barcode images

Barcode images may be saved in any of the supported graphics formats (BMP, GIF, JPG, PNG, TIF or WMF) by using the sBarcode() method:

er = dbcnet1.sBarcode(hres, vres, fname);

where fname is the full path name (including extension the identifies the graphic type) of the  file that will receive the image, and hres and vres are the pixel resolutions required for bitmap-type images. WMF images are saved as high resolution Windows metafiles.  The returned er is 0 if the file is created successfully, or non-zero otherwise.

Barcode images may also be saved to streams using

er = dbcnet1.sBarcode(hres, vres, frmt);

where frmt is one of the ImageFormat types, such as ImageFomat.Png

Bitmap-type graphic images created with a specified pixel resolution are adjusted so that bar widths are an integer number of pixels, so that when printed on a device with that resolution all bar widths will be correct.