flag german flag uk flag us Tip: Bookmark this page with Ctrl-D (Win) or Apple-D (Mac) MainProductsKnow How / FAQDownloadStoreContact



DISCONTINUED PRODUCT. PLEASE REFER TO OUR NEW .NET BARCODE LIBRARY AND CONTROL.

Manual - Forms Control Code EAN / ISBN 13 - C Sharp Sample

Following is a walkthrough of a sample project that demonstrates the use of the Code EAN / ISBN 13 control with C#. The project is located in the C# Sample folder in the installation directory.

Note

In order for the samples to work independently of the actual installation location, a copy of the control has been put into the bin\Debug folder of the sample project. Accordingly, the control is referenced in the References tab of the sample project to be in this location. For new projects this reference should point to the actual installation location. (If you followed the setup steps as outlined in the manual, dragging the control from the toolbox to a form will automatically point the reference to the correct location.)

Setting the control's properties

To update the barcode, simply set the control's properties and repaint it. Here's a code snippet from the sample. Each time the user hits the Refresh button, the barcode is updated:

  private void refresh_Click(object sender, EventArgs e)
  {
    codeEAN1.dataToEncode = dataToEncode.Text;
    codeEAN1.addon = addon.Text;
    codeEAN1.priceText = priceText.Text;
    codeEAN1.moduleHeight = System.Convert.ToSingle(moduleHeight.Text);
    codeEAN1.moduleWidth = System.Convert.ToSingle(moduleWidth.Text);
    codeEAN1.safeMode = (safeMode.CheckState == CheckState.Checked) ? true : false;
    codeEAN1.Invalidate();
  }

Note: The control can deduct from your data what code variant of EAN to use. For example, if your data has seven or eight digits, the control will create a Code EAN 8.

Saving the barcode

Saving a barcode to a file is simple, using the createCode function of the control. Here's the respective snippet from the sample. First, update the control's properties; then, all you have to provide is the required output resolution (in dpi):

  private void save_Click(object sender, EventArgs e)
  {
    this.refresh_Click(this, null);
    Bitmap code = codeEAN1.createCode(300.0f);
    SaveFileDialog dlg = new SaveFileDialog();
    dlg.Filter = "PNG Images (*.png)|*.png|TIFF Images (*.tif)|*.tif||";
    if (dlg.ShowDialog() == DialogResult.OK)
    {
      code.Save(dlg.FileName);
    }
  }


Printing the barcode

Printing the code first involves creating a PrintDocument and adding a handler for the actual printing function (this is boilerplate code for C# printing, nothing special). In the printing function, use the createCode function (providing the output resolution of the printer) and paint the code where you want to have it:

  private void print_Click(object sender, EventArgs e)
  {
    try
    {
      PrintDialog dlg = new PrintDialog();
      if (dlg.ShowDialog() == DialogResult.OK)
      {
        PrinterSettings ps = dlg.PrinterSettings;
        PrintDocument pd = new PrintDocument();
        pd.PrinterSettings = ps;
        pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
        pd.Print();
      }
    }
    catch (Exception ex)
    {
       // something went wrong
    }
  }

  private void pd_PrintPage(object sender, PrintPageEventArgs ev)
  {
    this.refresh_Click(this, null);
    Bitmap code = codeEAN1.createCode(ev.Graphics.DpiX);
    ev.Graphics.PageUnit = GraphicsUnit.Millimeter;
    ev.Graphics.DrawImage(code, new Point(20, 20));
    ev.HasMorePages = false;
  }


Using the bitpattern functions

If you want to do your own drawing (for example it you want to create an EAN without humen readable text), the control exposes the functions bitpattern_ean13, bitpattern_ean8 and bitpattern_addon that return the actual bitpattern of the main barcode and an addon. For every bar the array has a '1', for every space it has a '0'. You can then simply iterate through the array to paint the code. There are also two helper functions to calculate EAN check digits (see the reference).

Here's how to do it (simplified):

  ...

  WolfSoftware.Forms_Control_EAN.CodeEAN c = new WolfSoftware.Forms_Control_EAN.CodeEAN();
  char[] pattern = codeEAN1.bitpattern_ean13("4012345678901")

  for (int i = 0; i < pattern.Length; i++)
  {
    if(pattern[i] == '1')
    {
      // Paint a bar
    }
  }

  ...


The bitpattern routines do not use the control's properties; you'll have to provide the data to encode in the function call. The bitpattern routines will not perform any processing with regard to ISBNs; you have to strip hyphens, if any, and format the ISBN number correctly yourself.

Note: The bitpattern routines return an empty array if you are using the demo.

Using the control as a DLL / Library

While in most cases the control will be used on a form, you are not required to do so. Here's how to create a Code EAN barcode, using the control as a library:

  ...

  WolfSoftware.Forms_Control_EAN.CodeEAN c = new WolfSoftware.Forms_Control_EAN.CodeEAN();

  ... // setting of properties omitted

  Bitmap code = c.createCode(300.0f);
  code.Save("c:\\test.png");

  ...


Note that you will have to add a reference to the control manually if you are not using the control on a form.

Unlocking the control (removing the DEMO text)

To unlock the control (and remove the DEMO text in the barcode), you'll need the license information you received after completing your purchase in the store.

Simply call the unlock function of the control and provide your license info. It's recommended to do this right after the InitializeComponent call of the form:

  public Form1()
  {
    InitializeComponent();

    // Unlock and remove the DEMO text
    codeEAN1.unlock("LXXXXX-XXXXX", "XXXXX-XXXX-XXXXXXXX");
    
    // Other initializing stuff
    ...
  }

The unlock function returns a boolean that indicates if the unlock procedure was successful.