| Tip: Bookmark this page with Ctrl-D (Win) or Apple-D (Mac) | Main • Products • Know How / FAQ • Download • Store • Contact |
DISCONTINUED PRODUCT. PLEASE REFER TO OUR NEW .NET BARCODE LIBRARY AND CONTROL.
Manual - Forms Control Datamatrix - C Sharp Sample
Following is a walkthrough of a sample project that demonstrates the use of the Datamatrix 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)
{
datamatrix1.dataToEncode = dataToEncode.Text;
datamatrix1.moduleSize = System.Convert.ToSingle(moduleSize.Text);
datamatrix1.Invalidate();
}
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 = datamatrix1.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 = datamatrix1.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, if you want to create an inverted code), the control exposes a function, bitpattern_datamatrix, that returns the actual bitpattern of the barcode. For every module the array has a '1', for every space it has a '0'. Using the control's properties symbolRows and symbolCols you can then simply iterate through the array to paint the code.
Here's how to do it (simplified):
...
WolfSoftware.Forms_Control_Datamatrix.Datamatrix c = new WolfSoftware.Forms_Control_Datamatrix.Datamatrix();
char[] pattern = c.bitpattern_datamatrix("Datamatrix");
for (int i = 0; i < c.symbolRows; i++)
{
for (int j = 0; j < c.symbolCols; j++)
{
if(pattern[i * c.symbolCols + j] == '1')
{
// Paint a little square
}
}
}
...
The bitpattern routines do not use 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 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 Datamatrix barcode, using the control as a library:
...
WolfSoftware.Forms_Control_Datamatrix.Datamatrix c = new WolfSoftware.Forms_Control_Datamatrix.Datamatrix();
... // 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 from 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
datamatrix1.unlock("LXXXXX-XXXXX", "XXXXX-XXXX-XXXXXXXX");
// Other initializing stuff
...
}
The unlock function returns a boolean that indicates if the unlock procedure was successful.


