dLSoft  barcode & labelling software     

Generating barcodes in Delphi

For creating barcodes in the Delphi environment the component available for use with the dBarcode DLLs is ideal. The components are derived from TImage and so offer all the flexibility of the native component.

barcode in Delphi

Click to enlarge

Installation

The Tbarcodenn class is provided as source code is provided in a PAS file.

The component may be installed by installing the component as a PAS file and compiling the resulting user library.

The component’s icon (which is a TImage icon) will appear in the Samples page of the available components.

Using Tbarcode

The barcode component is based on the TImage and so inherits many of its properties from this object.  Over and above this, the component supports properties specific to its application, in this case the production of Barcodes.  The component can be placed on a form and its properties set a Design time or altered through code at Run-Time. 

A sample application is included with each product in the form of the dBarcode1 project, which includes the UNIT1.PAS source file. A typical example is shown below

procedure TForm1.DoBarcode();
begin
Barcode1.Xunit:=20;
Barcode1.BarcodeType:=8;
Barcode1.Caption:=Edit1.Text;
errormessage.Text:=Barcode1.GetError(Barcode1.Error);
end;

Printing the barcode

To print a barcode image one approach is to Draw the Picture on the Printer.Canvas, as shown in the code sample below.

Note that the Picture is created in HIMETRIC units, and so to print the image at the correct size these must be converted into printer pixel units using the GetDeviceCaps() function.

procedure TForm1.Button2Click(Sender: TObject);
var
        ii: integer;
        jj: integer;
        Rect: TRect;
        pScalex: Integer;
        pScaley: integer;
begin
      DoBarcode(); {recreate the barcode image}
      {set the target rectangle for drawing on the printer}
      {change from mm to pixels}
      pScalex:=GetDeviceCaps((Printer.Handle),LOGPIXELSX);
      pScaley:=GetDeviceCaps((Printer.Handle),LOGPIXELSY);
      ii:=(Barcode1.ImageWidth*pScalex) div 2540;
      jj:=(Barcode1.ImageHeight*pScaley) div 2540;
      Rect.Left:=200;
      Rect.Top:=200;
      Rect.Right:=Rect.Left+ii;
      Rect.Bottom:=Rect.Top+jj;
      Printer.BeginDoc;
      Printer.Canvas.StretchDraw(Rect, Barcode1.Picture.Graphic);
      Printer.EndDoc;
end;