dLSoft                software for barcodes and labelling                     

     HOME    PRODUCTS     DEVELOPERS     DOWNLOADS     SALES     SUPPORT     SERVICES     CONTACT US

Barcodes in .NET projects

dBarcode.NET components provide the ideal way of adding barcodes to any Visual Studio project and include components and WinForm Controls for Visual Studio 2005 or later, for the .NET Framework v 2.0 or later. The discussion here focuses on Windows Forms projects, but the components may be used in any .NET or ASP.NET project. For users who prefer non-CLR components dLSoft has a range of DLLs and Active DLLs for use with C++.

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 VB form
Screenshot - click to enlarge

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

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.

                     dBarcode1.Caption="0987654321";    C#
                     dBarcode1.Caption="0987654321"    VB

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:

                    dBarcode1.copyBarcode(hres.vres);    C#
                    dBarcode1.copyBarcode(hres.vres)    VB

                    dBarcode1.saveBarcode(hres,vres,filename);    C#
                    dBarcode1.saveBarcode(hres,vres,filename)    VB

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


Using the Component

If you prefer to use a component rather than the control....

Adding a dBarcode.NET component to a Visual Studio 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="12345"       in VB
        abcnet1.Caption="12345";      in 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      in VB
        x=abcnet1.Error;     in C#

Setting properties through the Barcode properties dialog box

Using the Method AbcProperties() causes the Barcode properties dialog to be displayed. 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()                    VB
{
      Dim g As Graphics = PictureBox1.CreateGraphics()
      Abcnet1.Caption="1234"
      PictureBox1.Image=Abcnet1.Barcode(g)
      g.Dispose()
}
private void DoBarcode()                  C#
{
      Graphics g=pictureBox1.CreateGraphics();
      abcnet391.Caption="1234";
      pictureBox1.Image=abcnet391.Barcode(g);
      g.Dispose();
}

 

Printing a barcode image

Printing the image returned by the Barcode() call may be accomplished by any of the printing techniques available for Visual Basic. 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:

VB

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
   e.Graphics.PageUnit = GraphicsUnit.Document '
   e.Graphics.DrawImage(Abcnet1.Barcode(e.Graphics), 300.0F, 300.0F)
   e.HasMorePages = False
End Sub

C#

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.