自己根據qrencode的源碼導了一個dll動態庫,見:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct QrCodeLL { public int version; ///< version of the symbol public int width; public int size;///< width of the symbol public IntPtr data; ///< symbol data }
[DllImport("LLQrencode.dll", EntryPoint = "OutPutQrCodeCSharp", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr OutPutQrCodeCSharp(byte[] bts);//接收參數對應 unsigned char* 也就是二維碼內容
使用方法:
public Form1() { InitializeComponent(); } string str = "https://www.cnblogs.com/HelloQLQ/p/16289395.html"; private void button1_Click(object sender, EventArgs e) { var byts = Encoding.UTF8.GetBytes(str); IntPtr ptr = IntPtr.Zero; var i = TestLL.OutPutQrCodeCSharp(byts); var qCode = Marshal.PtrToStructure<QrCodeLL>(i); int size = qCode.width * qCode.width + qCode.width - 1; byte[] managedArray = new byte[size]; Marshal.Copy(qCode.data, managedArray, 0, size); var arr = getPngArr(managedArray, qCode.width); var img = GetMap(arr, qCode.width); picBox.Image = Magnifier(img, 4); picBox.Refresh(); } public byte[] getPngArr(byte[] data, int width) { byte[] arr = new byte[data.Length]; int m = 0; for (int y = 0; y < width; y++) { for (int x = 0; x < width; x++) { if ((data[y * width + x] & 0x01) == 1) { arr[m++] = 0; } else { arr[m++] = 1; } } } return arr; } public Bitmap GetMap(byte[] arr, int w) { Bitmap map = new Bitmap(w, w); for (int i = 0; i < w; i++) { for (int j = 0; j < w; j++) { if (arr[i * w + j] == 1) map.SetPixel(i, j, Color.White); else { map.SetPixel(i, j, Color.Black); } } } return map; } public Bitmap Magnifier(Bitmap srcbitmap, int multiple) { if (multiple <= 0) { multiple = 0; return srcbitmap; } Bitmap bitmap = new Bitmap(srcbitmap.Size.Width * multiple, srcbitmap.Size.Height * multiple); BitmapData srcbitmapdata = srcbitmap.LockBits(new Rectangle(new Point(0, 0), srcbitmap.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); BitmapData bitmapdata = bitmap.LockBits(new Rectangle(new Point(0, 0), bitmap.Size), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); unsafe { byte* srcbyte = (byte*)(srcbitmapdata.Scan0.ToPointer()); byte* sourcebyte = (byte*)(bitmapdata.Scan0.ToPointer()); for (int y = 0; y < bitmapdata.Height; y++) { for (int x = 0; x < bitmapdata.Width; x++) { long index = (x / multiple) * 4 + (y / multiple) * srcbitmapdata.Stride; sourcebyte[0] = srcbyte[index]; sourcebyte[1] = srcbyte[index + 1]; sourcebyte[2] = srcbyte[index + 2]; sourcebyte[3] = srcbyte[index + 3]; sourcebyte += 4; } } } srcbitmap.UnlockBits(srcbitmapdata); bitmap.UnlockBits(bitmapdata); return bitmap; }