Create DataMatrix with a Component Object Model (COM) Library
This tutorial illustrates the use of a COM (Component Object Model) object library with a True Type Font (DataMatrix Barcode Font), provided in ConnectCode DataMatrix package, to create a ISO/IEC 16022:2006 (ECC200) standard-compliant DataMatrix in a .NET Windows Form application.
- ConnectCode DataMatrix package is installed
- DataMatrixCOMLibrary.dll in the Resource\DataMatrixCOMLibrary subdirectory of ConnectCode DataMtrix package.
The DataMatrix class library has been compiled with the "Register for COM interop" Visual Studio project property, exposing a COM-callable wrapper that enables COM interaction.
- Visual Studio 2015/2017/2019
- Administrator Rights
1. Launch the Visual Studio Developer Command Prompt as Administrator from the Windows Start Menu. In the Command Prompt, use the "cd" command to go to the DataMatrix COM Library folder.
cd C:\Program Files (x86)\ConnectCodeDataMatrix\Resource\DataMatrixCOMLibrary (or ConnectCodeDataMatrixTrial if you are using the Trial package)
2. Enter the following command in the Developer Command Prompt to register the DataMatrix COMLibrary assembly for use with COM.
Regasm.exe adds information about the class to the system registry so that COM clients can use the .NET Framework class transparently. The tlb option generates a type library defined within the assembly.
3. Launch Visual Studio and create a new Windows Form project by clicking on "File -> New Project", selecting a "Windows Forms App" and clicking on the "Create" button.
4. Double click on "Form1.cs" in the "Solution Explorer" and add a "Button" and a "RichTextBox" from the Visual Studio Toolbox. You should see your Windows Form like the screenshot below. We will set up the programming codes so that when we click on the button, the DataMatrix COM library will be loaded to encode an input string. The result of the encodation will be placed in the "RichTextBox" and applied with a DataMatrix barcode font.
5. Right click on the "WindowsFormApp1" project in the "Solution Explorer" and select "Add -> Existing Item". Navigate to the "C:\Program Files (x86)\ConnectCodeDataMatrix\" folder and select the "CCodeDataMatrix.ttf" font. If you are using the trial version, select the "CCodeDataMatrix_Trial.ttf" font instead.
6. In "Solution Explorer", select the "CCodeDataMatrix.ttf" font and change the "Build Action" to "Content" and "Copy to Output Directory" to "Copy Always" in the Properties pane. This will ensure that Visual Studio deploy the DataMatrix barcode font for use with the Windows Form application.
7. Double click on "Form1.cs" in the "Solution Explorer". In the designer, double click on the Button. This will generate the button1_Click function in the editor. Enter the C# programming codes as shown below:
The C# function above creates a DataMatrix COM object and then uses it to generate a DataMatrix barcode with the input data "12345678". The result is placed in "richTextBox1" and the final output is displayed with the CCodeDataMatrix.ttf True Type font. When you run the application, click on the "Encode with COM" button, you should see the DataMatrix barcode as shown below.
The "DataMatrixCOMApplication" folder in "C:\Program Files (x86)\ConnectCodeDataMatrix\Resource" contains the full source code of the above application. The "DataMatrixCOMLibrary.dll" can be disributed with your application with an Enterprise or Distribution License.