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

Manual - .NET Barcode Library - C# Sample

Following is a walkthrough of a sample project that demonstrates the use of the .NET Barcode Control with C#. Usage in VB .net is virtually identically. The project is located in the Sample folder of the downloadable archive.

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. (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)
  {
    barcodeControl.DataToEncode = dataToEncode.Text;
    barcodeControl.Addon = addon.Text;
    barcodeControl.ModuleHeight = System.Convert.ToSingle(moduleHeight.Text);
    barcodeControl.ModuleWidth = System.Convert.ToSingle(moduleWidth.Text);
    barcodeControl.Ratio = System.Convert.ToSingle(ratio.Text);
  }


Saving the barcode

Saving a barcode to a file is simple, using the GetCode 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 = barcodeControl.GetCode(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

The folloing snippet is not part of the sample, it's included here to save you some time and typing. 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 GetCode function (providing the output resolution of the printer context) 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 = barcodeControl.GetCode(ev.Graphics.DpiX);
    ev.Graphics.PageUnit = GraphicsUnit.Millimeter;
    ev.Graphics.DrawImage(code, new Point(20, 20));
    ev.HasMorePages = false;
  }


Using the bitpattern functions

Note: This functionality requires the source code of the library.

If you want to do your own drawing, the L1 level of the included barcode reference implementation exposes several functions that return the actual bitpattern of the respective barcode. You find the bitpattern functions in the class files of the respective barcode, e.g. for EAN the routines are at L1/ean/CodeEAN.cs. The classes in the L1 level are in the WolfSoftware.Reference namespace and can be used independently from the actual control.

For every bar the returned array has a '1', for every space it has a '0'. You can then simply iterate through the array to paint the code.

Here's how to do it (simplified):

  ...

  WolfSoftware.Reference.CodeEAN = new WolfSoftware.Reference.CodeEAN();
  char[] pattern = ean.bitpattern_ean13("401234567890")

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

  ...


The bitpattern routines do not use any of the control's properties; you'll have to provide the data to encode in the function call.

Note: The bitpattern routines return an empty array if you are trying to encode invalid data, e.g. letters with Code EAN.

Using the control as a DLL / Library in ASP / .NET

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.Library_NET.BarcodeControl b = new WolfSoftware.Library_NET.BarcodeControl();
  b.CurrentCode = 1001; // 1001 = EAN 13
  b.DataToEncode = "401234567890";
  b.CurrentEANSize = 2; // 2 = "SC 2", 100% size

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

  ...


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

Thus the control can be used in an ASP environment, see our barcode online server for an example usage.

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();

    // Init barcode control;
    barcodeControl.Init();

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

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


Back to the manual index.