生成二維碼,儲存成圖片
阿新 • • 發佈:2021-07-07
/// <summary> /// 生成二維碼,儲存成圖片,使用了ZXing.Net /// </summary> static byte[] GenerateQRimage(string content) { //初始化條形碼格式,寬高,以及PureBarcode=true則不會留白框 var writer = new BarcodeWriterPixelData { Format = BarcodeFormat.CODABAR, Options= new EncodingOptions { Height = 61, Width = 267, PureBarcode = true, Margin = 1 } }; var pixelData = writer.Write(content); using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)) using (varms = new MemoryStream()) { var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); try { System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels,0, bitmapData.Scan0, pixelData.Pixels.Length); } finally { bitmap.UnlockBits(bitmapData); } bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] bytes = ms.GetBuffer(); return bytes; } }