Softmatic .NET Barcode Library - C# Barcode Generator

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. Most of the relevant code is in the file Form1.cs which is extensively commented.

Screenshot of the completed sample, showing an EAN 13 barcode created with C#:

Screenshot of C# Barcode Generator

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 Barcode 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 to a File

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 Barcode 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:

  ...

  Softmatic.Library_NET.BarcodeControl b = new Softmatic.Library_NET.BarcodeControl();
  b.Init(); // Initialize to defaults
  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.


Back to the manual index.