你的位置:首页 > ASP.net教程

[ASP.net教程]如何 在 .net 下使用 zint 生成条形码


1、首先你需要编译好的 zint 条码库。什么,你没有?那没关系,请参看:在 win32 下编译 zint 条码库。

2、启动 VISUAL STUDIO,新建一 “Windows Forms Application”,将 zint.dll、zlib1.dll 和 libpng15.dll 添加到项目中,属性窗口中设置“Copy to Output Directory”为”Copy always”。

3、打开“Form1.cs”代码文件,声明 zint:

 

    /// <summary>    /// 条形码编码类型。    /// </summary>    public enum Symbology    {      BARCODE_CODE11 = 1,      BARCODE_C25MATRIX = 2,      BARCODE_C25INTER = 3,      BARCODE_C25IATA = 4,      BARCODE_C25LOGIC = 6,      BARCODE_C25IND = 7,      BARCODE_CODE39 = 8,      BARCODE_EXCODE39 = 9,      BARCODE_EANX = 13,      BARCODE_EAN128 = 16,      BARCODE_CODABAR = 18,      BARCODE_CODE128 = 20,      BARCODE_DPLEIT = 21,      BARCODE_DPIDENT = 22,      BARCODE_CODE16K = 23,      BARCODE_CODE49 = 24,      BARCODE_CODE93 = 25,      BARCODE_FLAT = 28,      BARCODE_RSS14 = 29,      BARCODE_RSS_LTD = 30,      BARCODE_RSS_EXP = 31,      BARCODE_TELEPEN = 32,      BARCODE_UPCA = 34,      BARCODE_UPCE = 37,      BARCODE_POSTNET = 40,      BARCODE_MSI_PLESSEY = 47,      BARCODE_FIM = 49,      BARCODE_LOGMARS = 50,      BARCODE_PHARMA = 51,      BARCODE_PZN = 52,      BARCODE_PHARMA_TWO = 53,      BARCODE_PDF417 = 55,      BARCODE_PDF417TRUNC = 56,      BARCODE_MAXICODE = 57,      BARCODE_QRCODE = 58,      BARCODE_CODE128B = 60,      BARCODE_AUSPOST = 63,      BARCODE_AUSREPLY = 66,      BARCODE_AUSROUTE = 67,      BARCODE_AUSREDIRECT = 68,      BARCODE_ISBNX = 69,      BARCODE_RM4SCC = 70,      BARCODE_DATAMATRIX = 71,      BARCODE_EAN14 = 72,      BARCODE_CODABLOCKF = 74,      BARCODE_NVE18 = 75,      BARCODE_JAPANPOST = 76,      BARCODE_KOREAPOST = 77,      BARCODE_RSS14STACK = 79,      BARCODE_RSS14STACK_OMNI = 80,      BARCODE_RSS_EXPSTACK = 81,      BARCODE_PLANET = 82,      BARCODE_MICROPDF417 = 84,      BARCODE_ONECODE = 85,      BARCODE_PLESSEY = 86,      BARCODE_TELEPEN_NUM = 87,      BARCODE_ITF14 = 89,      BARCODE_KIX = 90,      BARCODE_AZTEC = 92,      BARCODE_DAFT = 93,      BARCODE_MICROQR = 97,      BARCODE_HIBC_128 = 98,      BARCODE_HIBC_39 = 99,      BARCODE_HIBC_DM = 102,      BARCODE_HIBC_QR = 104,      BARCODE_HIBC_PDF = 106,      BARCODE_HIBC_MICPDF = 108,      BARCODE_HIBC_BLOCKF = 110,      BARCODE_HIBC_AZTEC = 112,      BARCODE_AZRUNE = 128,      BARCODE_CODE32 = 129,      BARCODE_EANX_CC = 130,      BARCODE_EAN128_CC = 131,      BARCODE_RSS14_CC = 132,      BARCODE_RSS_LTD_CC = 133,      BARCODE_RSS_EXP_CC = 134,      BARCODE_UPCA_CC = 135,      BARCODE_UPCE_CC = 136,      BARCODE_RSS14STACK_CC = 137,      BARCODE_RSS14_OMNI_CC = 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、在窗体上绘制条码:

    protected override void OnPaint(PaintEventArgs e)    {      // 初始化编码器      ZintSymbolStruct symbol = (ZintSymbolStruct)Marshal.PtrToStructure(InitZint(), typeof(ZintSymbolStruct));      // 设置编码类型      symbol.symbology = Symbology.BARCODE_QRCODE;      // 缩放      symbol.scale = 5;      // 编码内容      string s = "0123456789";      // 编码      ZintEncode(ref symbol, s, 0, 0);      // 创建一个位图,以便绘制条码      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 bmpData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);      IntPtr ptr = bmpData.Scan0;      int bytes = bmpData.Stride * bitmap.Height;      byte[] rgbValues = new byte[bytes];      Marshal.Copy(ptr, rgbValues, 0, bytes);      // Zint 编码生成的位图数据存放格式为 RGB,需要将其转换为 BMP 数据存放格式 BGR      int row, col;      byte red, green, blue;      int i = 0;      int l = 0;      for (row = 0; row < symbol.bitmap_height; row++)      {        l = bmpData.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(bmpData);      e.Graphics.DrawImage(bitmap, 12, 12);    }
5、将目标平台更改为“x86”,运行后即可查看效果。

 

附源码下载。