Generate Data Matrix barcodes in C# .NET
Nutrient .NET SDK (formerly GdPicture.NET) enables you to generate one-dimensional (1D or linear) and two-dimensional (2D) barcodes.
Nutrient .NET SDK supports all 1D barcode formats and the following 2D barcode formats:
- Aztec Code
- Data Matrix
- MaxiCode
- Micro QR
- PDF417
- QR
To generate a Data Matrix barcode, follow the steps below:
- Create a
GdPictureImagingobject. - Set the encoding mode of the barcode with the
BarcodeDataMatrixEncodingModeenumeration, and store it in a variable. UseBarcodeDataMatrixEncodingModeUndefinedto let the engine determine the most appropriate encoding mode for the input data. - Set the version of the barcode with the
BarcodeDataMatrixVersionenumeration, and store it in a variable. UseBarcodeDataMatrixVersionAutoto let the engine determine the minimum version required to encode all input data. - Compute the size of the barcode with the
BarcodeDataMatrixGetSizemethod of theGdPictureImagingobject. This method takes the following parameters:- The data encoded in the barcode.
- The encoding mode.
- The version of the barcode.
- The number of modules considered quiet zone around the barcode. The recommended value is
4or more. - The size of each module in pixels. The recommended value is
4or more. - An output parameter that defines the width of the barcode.
- An output parameter that defines the height of the barcode.
- Create an empty GdPicture image with the
CreateNewGdPictureImagemethod of theGdPictureImagingobject. This method takes the following parameters:- The width of the image in pixels.
- The height of the image in pixels.
- The bit depth of the image.
- The background color of the image. Use the
ARGBmethod of theGdPictureImagingobject to specify the color with the ARGB standard. For example, for a white background, setgdpictureImaging.ARGB(255, 255, 255).
- Write the barcode to the empty image with the
BarcodeDataMatrixWritemethod of theGdPictureImagingobject. This method takes the following parameters:- The image ID of the newly created image.
- The data encoded in the barcode.
- The encoding mode.
- The version of the barcode.
- The number of modules considered quiet zone around the barcode. The recommended value is
4or more. - The size of each module in pixels. The recommended value is
4or more. - The distance between the left side of the image and the barcode in pixels.
- The distance between the top side of the image and the barcode in pixels.
- The angle of the barcode.
- The color of the symbols on the barcode. Use the
ARGBmethod of theGdPictureImagingobject to specify the color with the ARGB standard. For example, for black, setgdpictureImaging.ARGB(0, 0, 0). - The background color of the barcode. Use the
ARGBmethod of theGdPictureImagingobject to specify the color with the ARGB standard. For example, for white, setgdpictureImaging.ARGB(255, 255, 255).
- Save the image as a PNG file with the
SaveAsPNGmethod of theGdPictureImagingobject. - Release unnecessary resources.
The example below generates a Data Matrix barcode:
using GdPictureImaging gdpictureImaging = new GdPictureImaging();// Set the encoding mode of the barcode.BarcodeDataMatrixEncodingMode encodingMode = BarcodeDataMatrixEncodingMode.BarcodeDataMatrixEncodingModeUndefined;// Set the version of the barcode.BarcodeDataMatrixVersion version = BarcodeDataMatrixVersion.BarcodeDataMatrixVersionAuto;// Compute the size of the barcode.gdpictureImaging.BarcodeDataMatrixGetSize("0123456789", encodingMode, ref version, 4, 8, out int width, out int height);// Create an empty GdPicture image.int imageID = gdpictureImaging.CreateNewGdPictureImage(width, height, 32, gdpictureImaging.ARGB(255, 255, 255));// Write the barcode to the empty image.gdpictureImaging.BarcodeDataMatrixWrite(imageID, "0123456789", encodingMode, ref version, 4, 8, 0, 0, 0, gdpictureImaging.ARGB(0, 0, 0), gdpictureImaging.ARGB(255, 255, 255));// Save the image as a PNG file.gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png");// Release unnecessary resources.gdpictureImaging.ReleaseGdPictureImage(imageID);Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() ' Set the encoding mode of the barcode. Dim encodingMode As BarcodeDataMatrixEncodingMode = BarcodeDataMatrixEncodingMode.BarcodeDataMatrixEncodingModeUndefined ' Set the version of the barcode. Dim version As BarcodeDataMatrixVersion = BarcodeDataMatrixVersion.BarcodeDataMatrixVersionAuto ' Compute the size of the barcode. Dim width As Integer = Nothing, height As Integer = Nothing gdpictureImaging.BarcodeDataMatrixGetSize("0123456789", encodingMode, version, 4, 8, width, height) ' Create an empty GdPicture image. Dim imageID As Integer = gdpictureImaging.CreateNewGdPictureImage(width, height, 32, gdpictureImaging.ARGB(255, 255, 255)) ' Write the barcode to the empty image. gdpictureImaging.BarcodeDataMatrixWrite(imageID, "0123456789", encodingMode, version, 4, 8, 0, 0, 0, gdpictureImaging.ARGB(0, 0, 0), gdpictureImaging.ARGB(255, 255, 255)) ' Save the image as a PNG file. gdpictureImaging.SaveAsPNG(imageID, "C:\temp\output.png") ' Release unnecessary resources. gdpictureImaging.ReleaseGdPictureImage(imageID)End UsingUsed methods and properties
Related topics