How to Use zint in. net to generate a bar code,. netzint
1. First, you need to compile the zint library. What, you don't? That's okay. See compile the zint library under win32.
2. Start visual studio, create a "Windows Forms Application", and set zint. dll, zlib1.dll, and libpng15.dll are added to the project. In the Properties window, set "Copy to Output Directory" to "Copy always ".
3. Open the "Form1.cs" code file and declare zint:
/// <Summary> /// barcode encoding type. /// </Summary> public enum Symbology {BARCODE_CODE11 = 1, BARCODE_C25MATRIX = 2, priority = 3, priority = 4, BARCODE_C25LOGIC = 6, BARCODE_C25IND = 7, BARCODE_CODE39 = 8, BARCODE_EXCODE39 = 9, clerk = 13, clerk = 16, BARCODE_CODABAR = 18, BARCODE_CODE128 = 20, clerk = 21, clerk = 22, BARCODE_CODE16K = 23, BARCODE_CODE49 = 24, BARCODE_CODE93 = 25, BARCODE_FLAT = 28, BARCODE_RSS14 = 29, cost = 30, cost = 31, BARCODE_TELEPEN = 32, BARCODE_UPCA = 34, cost = 37, BARCODE_POSTNET = 40, cost = 47, BARCODE_FIM = 49, BARCODE_LOGMARS = 50, BARCODE_PHARMA = 51, BARCODE_PZN = 52, clerk = 53, clerk = 55, clerk = 56, BARCODE_MAXICODE = 57, clerk = 58, BARCODE_CODE128B = 60, BARCODE_AUSPOST = 63, counter = 66, counter = 67, counter = 68, BARCODE_ISBNX = 69, BARCODE_RM4SCC = 70, counter = 71, BARCODE_EAN14 = 72, counter = 74, BARCODE_NVE18 = 75, BARCODE_JAPANPOST = 76, counter = 77, BARCODE_RSS14STACK = 79, counter = 80, counter = 81, BARCODE_PLANET = 82, barcode_micro1_417 = 84, BARCODE_ONECODE = 85, BARCODE_PLESSEY = 86, counter = 87, BARCODE_ITF14 = 89, BARCODE_KIX = 90, BARCODE_AZTEC = 92, BARCODE_DAFT = 93, BARCODE_MICROQR = 97, BARCODE_HIBC_128 = 98, clerk = 99, clerk = 102, clerk = 104, BARCODE_HIBC_PDF = 106, clerk = 108, BARCODE_HIBC_BLOCKF = 110, BARCODE_HIBC_AZTEC = 112, BARCODE_AZRUNE = 128, BARCODE_CODE32 = 129, clerk = 130, clerk = 131, clerk = 132, clerk = 133, clerk = 134, BARCODE_UPCA_CC = 135, BARCODE_UPCE_CC = 136, BARCODE_RSS14STACK_CC = 137, clerk = 138, BARCODE_RSS_EXPSTACK_CC = 139, BARCODE_CHANNEL = 140, BARCODE_CODEONE = 141, BARCODE_GRIDMATRIX = 142}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct ZintSymbolStruct { public Symbology symbology; public int height; public int whitespace_width; public int border_width; public int output_options; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)] public string fgcolour; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)] public string bgcolour; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string outfile; public float scale; public int option_1; public int option_2; public int option_3; public int show_hrt; public int input_mode; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string text; public int rows; public int width; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string primary; [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = 25454)] public byte[] encoded_data; [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I4, SizeConst = 178)] public int[] row_height; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)] public string errtxt; public IntPtr bitmap; public int bitmap_width; public int bitmap_height; public IntPtr rendered; }
[DllImport("zint.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "ZBarcode_Create")] public extern static IntPtr InitZint(); [DllImport("zint.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "ZBarcode_Encode_and_Buffer", CharSet = CharSet.Ansi)] public extern static int ZintEncode( ref ZintSymbolStruct symbol, string input, int length, int rotate_angle); [DllImport("zint.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "ZBarcode_Encode_and_Print", CharSet = CharSet.Ansi)] public extern static int ZintEncodeAndPrint( ref ZintSymbolStruct symbol, string input, int length, int rotate_angle);
4. Draw a bar code on the form:
Protected override void OnPaint (PaintEventArgs e) {// initialize the ZintSymbolStruct symbol = (ZintSymbolStruct) Marshal. ptrToStructure (InitZint (), typeof (ZintSymbolStruct); // you can specify the encoding type of the symbol. symbology = Symbology. BARCODE_QRCODE; // zoom in/out symbol. scale = 5; // encode the content string s = "0123456789"; // encode ZintEncode (ref symbol, s, 0, 0); // create a bitmap, to draw a bar code byte [] bitmapData = new byte [symbol. bitmap_width * symbol. bitmap_height * 3]; Marshal. copy (symbol. bitmap, bitmapData, 0, bitmapData. length); Bitmap bitmap = new Bitmap (symbol. bitmap_width, symbol. bitmap_height, System. drawing. imaging. pixelFormat. format24bppRgb); Rectangle rect = new Rectangle (0, 0, bitmap. width, bitmap. height); System. drawing. imaging. bitmapData bmp data = bitmap. lockBits (rect, System. drawing. imaging. imageLockMode. readWrite, bitmap. pixelFormat); IntPtr = BMP data. scan0; int bytes = BMP data. stride * bitmap. height; byte [] rgbValues = new byte [bytes]; Marshal. copy (ptr, rgbValues, 0, bytes); // The bitmap data generated by Zint encoding is saved in RGB format. You need to convert it to the BMP Data Storage Format BGR int row, col; byte red, green, blue; int I = 0; int l = 0; for (row = 0; row <symbol. bitmap_height; row ++) {l = BMP data. stride * row; for (col = 0; col <symbol. bitmap_width; col ++) {red = bitmapData [I]; green = bitmapData [I + 1]; blue = bitmapData [I + 2]; rgbValues [l] = blue; rgbValues [l + 1] = green; rgbValues [l + 2] = red; I + = 3; l + = 3 ;}} System. runtime. interopServices. marshal. copy (rgbValues, 0, ptr, bytes); bitmap. unlockBits (BMP data); e. graphics. drawImage (bitmap, 12, 12 );}
5. Change the target platform to "x86", and then you can view the effect.
Download the source code.