/// <summary> /// 创建二维码,添加小图标,也可以不添加 /// </summary> /// <param name="content">二维码的内容</param> /// <param name="qrWidth">二维码的宽度</param> /// <param name="qrHeight">二维码的高度</param> /// <param name="middleTex">添加的小图标</param> /// <returns>二维码生成的Texture2D</returns> public static Texture2D GeneQRwithString2(string content, int qrWidth, int qrHeight, Texture2D middleTex = null) { // 将 content 编码成 bit矩阵 Dictionary <EncodeHintType, object> hints = new Dictionary <EncodeHintType, object>(); hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); // 编码 hints.Add(EncodeHintType.MARGIN, 2); // 边缘距离 hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 纠错方式 MultiFormatWriter writer = new MultiFormatWriter(); BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, qrWidth, qrHeight, hints); // bit 矩阵 Texture2D tex = new Texture2D(qrWidth, qrHeight); for (int y = 0; y < qrHeight; y++) { for (int x = 0; x < qrWidth; x++) { if (bitMatrix[x, y]) { tex.SetPixel(x, qrHeight - y, Color.black); // true 的地方 不能用白色 } else { tex.SetPixel(x, qrHeight - y, Color.white); // false 的位置 不能用黑色 } } } // 在 Tex2D的中间添加图片,是不会影响检测的 if (middleTex != null) { AddTex(middleTex, tex.width / 2f, tex.height / 2f, tex); } tex.Apply(); return(tex); }
/// <summary> /// Encode the specified string . /// </summary> /// <param name="valueStr"> content string.</param> public void Encode(string valueStr) { // var writer = new QRCodeWriter(); var writer = new MultiFormatWriter(); Dictionary <EncodeHintType, object> hints = new Dictionary <EncodeHintType, object>(); //set the code type hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); byteMatrix = writer.encode(valueStr, BarcodeFormat.QR_CODE, e_QRCodeWidth, e_QRCodeHeight, hints); //writer1.encode("ddd",BarcodeFormat. for (int i = 0; i != e_QRCodeWidth; i++) { for (int j = 0; j != e_QRCodeHeight; j++) { if (byteMatrix[i, j]) { m_EncodedTex.SetPixel(i, j, Color.black); } else { m_EncodedTex.SetPixel(i, j, Color.white); } } } ///rotation the image Color32[] pixels = m_EncodedTex.GetPixels32(); pixels = RotateMatrixByClockwise(pixels, m_EncodedTex.width); m_EncodedTex.SetPixels32(pixels); m_EncodedTex.Apply(); e_QREncodeFinished(m_EncodedTex); }
//二维码 private void btnEncode_Click(object sender, EventArgs e) { labShow.Text = ""; string content = txtMsg.Text.Trim(); if (String.IsNullOrEmpty(content)) { MessageBox.Show("请输入原文!"); return; } try { MultiFormatWriter writer = new MultiFormatWriter(); Hashtable hints = new Hashtable(); hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); ByteMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, 300, 300); Bitmap bitmap = matrix.ToBitmap(); pictureBox1.Image = bitmap; //自动保存图片到当前目录 //string filename = Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString() + ".jpg"; //bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg); //labShow.Text = "图片已保存到:" + filename; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 显示绘制的二维码,任意尺寸正方形 /// </summary> /// <param name="_str">扫码信息</param> Sprite ShowQRCode(string _str, int _width, int _height) { MultiFormatWriter writer = new MultiFormatWriter(); Dictionary <EncodeHintType, object> hints = new Dictionary <EncodeHintType, object>() { { EncodeHintType.CHARACTER_SET, "UTF-8" }, { EncodeHintType.MARGIN, 1 }, { EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.M }, }; BitMatrix bitMatrix = writer.encode(_str, BarcodeFormat.QR_CODE, _width, _height, hints); int w = bitMatrix.Width; int h = bitMatrix.Height; Texture2D texture = new Texture2D(_width, _height); for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { if (bitMatrix[x, y]) { texture.SetPixel(y, x, Color.black); } else { texture.SetPixel(y, x, Color.white); } } } texture.Apply(); //创景精灵返回 return(Sprite.Create(texture, new Rect(0, 0, _width, _height), new Vector2(0.5f, 0.5f))); }
private Bitmap GeneratorQrImage(string contents) { MultiFormatWriter mutiWriter = new MultiFormatWriter(); BitMatrix matrix = mutiWriter.encode(contents, BarcodeFormat.QR_CODE, 300, 300); int width = matrix.Width; int height = matrix.Height; //二维矩阵转为一维像素数组,也就是一直横着排了 int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (matrix[x, y]) { pixels[y * width + x] = System.BitConverter.ToInt32(System.BitConverter.GetBytes(0xff000000), 0);; } } } Bitmap bitmap = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888); //通过像素数组生成bitmap,具体参考api bitmap.SetPixels(pixels, 0, width, 0, 0, width, height); return(bitmap); }
public static System.Drawing.Bitmap Create(string content) { MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); ByteMatrix byteMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 300, 300); return(byteMatrix.ToBitmap()); }
public static Texture2D GenerateQRImageConstantSize(string content, int width, int height) { // 编码成color32 MultiFormatWriter writer = new MultiFormatWriter(); Dictionary <EncodeHintType, object> hints = new Dictionary <EncodeHintType, object>(); hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.Add(EncodeHintType.MARGIN, 1); hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.M); BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints); // 转成texture2d int w = bitMatrix.Width; int h = bitMatrix.Height; Texture2D texture = new Texture2D(w, h); for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { if (bitMatrix[x, y]) { //可在此改颜色 texture.SetPixel(y, x, Color.black); } else { texture.SetPixel(y, x, Color.white); } } } texture.Apply(); return(texture); }
public static System.Drawing.Bitmap Create(string content, System.Drawing.Image centralImage) { MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); System.Collections.Hashtable hashtable = new System.Collections.Hashtable(); hashtable.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hashtable.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); ByteMatrix byteMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 300, 300, hashtable); System.Drawing.Bitmap bitmap = byteMatrix.ToBitmap(); System.Drawing.Size encodeSize = multiFormatWriter.GetEncodeSize(content, BarcodeFormat.QR_CODE, 300, 300); int num = System.Math.Min((int)((double)encodeSize.Width / 3.5), centralImage.Width); int num2 = System.Math.Min((int)((double)encodeSize.Height / 3.5), centralImage.Height); int x = (bitmap.Width - num) / 2; int y = (bitmap.Height - num2) / 2; System.Drawing.Bitmap bitmap2 = new System.Drawing.Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap2)) { graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.DrawImage(bitmap, 0, 0); } System.Drawing.Graphics graphics2 = System.Drawing.Graphics.FromImage(bitmap2); graphics2.FillRectangle(System.Drawing.Brushes.White, x, y, num, num2); graphics2.DrawImage(centralImage, x, y, num, num2); return(bitmap2); }
/// <summary> /// 生成二维码 /// </summary> /// <param name="strContent">二维码文本</param> /// <param name="iWidth">二维码宽度</param> /// <param name="iHeigth">二维码高度</param> /// <returns>二维码位图</returns> public static Bitmap GetQRCode(string strContent, int iWidth, int iHeigth) { try { //构造二维码写码器 MultiFormatWriter writer = new MultiFormatWriter(); Dictionary <EncodeHintType, object> hint = new Dictionary <EncodeHintType, object>(); hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hint.Add(EncodeHintType.MARGIN, 1); //生成二维码 BitMatrix bitMatrix = writer.encode(strContent, BarcodeFormat.QR_CODE, iWidth, iHeigth, hint); BarcodeWriter barcodeWriter = new BarcodeWriter(); Bitmap bitmapQRCode = barcodeWriter.Write(bitMatrix); //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) int[] rectangle = bitMatrix.getEnclosingRectangle(); //将img转换成bmp格式,否则后面无法创建Graphics对象 Bitmap bitmapQRCodeBMP = new Bitmap(bitmapQRCode.Width, bitmapQRCode.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bitmapQRCodeBMP)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(bitmapQRCode, 0, 0); } return(bitmapQRCodeBMP); } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return(null); } }
//生成条形码 private void btnBarCode_Click(object sender, EventArgs e) { labShow.Text = ""; string content = txtMsg.Text.Trim(); //if (content.Length != 13) //{ // MessageBox.Show("请输入13位的字符!"); // return; //} Regex regex = new Regex("^[0-9]{13}$"); if (!regex.IsMatch(content)) { MessageBox.Show("请输入13位的数字!"); return; } try { MultiFormatWriter writer = new MultiFormatWriter(); ByteMatrix matrix = writer.encode(content, BarcodeFormat.EAN_13, 360, 150); Bitmap bitmap = matrix.ToBitmap(); pictureBox1.Image = bitmap; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 创建中间带有logo的二维码图片,返回base64字符串 /// </summary> /// <param name="contents">要生成二维码包含的信息</param> /// <param name="logoPath">二维码中间的logo图片路径</param> /// <param name="width">二维码宽度</param> /// <param name="height">二维码高度</param> /// <param name="imageFormat">图片格式,默认:png</param> /// <returns>bool</returns> public static string CreateQrBase64(string contents, string logoPath, int width = 300, int height = 300, ImageFormat imageFormat = null) { if (!string.IsNullOrEmpty(contents)) { if (!string.IsNullOrEmpty(logoPath)) { using (var logoImage = Image.FromFile(logoPath)) { //构造二维码写码器 var mutiWriter = new MultiFormatWriter(); var hint = new Dictionary <EncodeHintType, object> { [EncodeHintType.CHARACTER_SET] = "UTF-8", [EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.H }; //生成二维码 var bm = mutiWriter.encode(contents, BarcodeFormat.QR_CODE, width, height, hint); var barcodeWriter = new BarcodeWriter(); using (var bitmap = barcodeWriter.Write(bm)) { //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) var rectangle = bm.getEnclosingRectangle(); //计算插入图片的大小和位置 var middleImgW = Math.Min((int)(rectangle[2] / 3.5), logoImage.Width); var middleImgH = Math.Min((int)(rectangle[3] / 3.5), logoImage.Height); var middleImgL = (bitmap.Width - middleImgW) / 2; var middleImgT = (bitmap.Height - middleImgH) / 2; //将img转换成bmp格式,否则后面无法创建 Graphics对象 using (var bmpimg = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb)) { using (var g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.SmoothingMode = SmoothingMode.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality; g.DrawImage(bitmap, 0, 0); } //在二维码中插入图片 using (var myGraphic = Graphics.FromImage(bmpimg)) { //白底 myGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH); myGraphic.DrawImage(logoImage, middleImgL, middleImgT, middleImgW, middleImgH); } using (var ms = new MemoryStream()) { bmpimg.Save(ms, imageFormat ?? ImageFormat.Png); return($"data:image/{(imageFormat == ImageFormat.Jpeg ? "jpeg" : "png")};base64,{Convert.ToBase64String(ms.ToArray())}"); } } } } } else { return(CreateQrBase64(contents, width, height, imageFormat)); } } return(null); }
public static string CreateQRCodePic(string info, string midImgPath, string saveDir, string fileName, ErrorCorrectionLevel ecLevel, int width = 300, int height = 300) { string QRImgPath = string.Empty; if (string.IsNullOrEmpty(info))//要存储的信息不能为空,否则异常 { return(QRImgPath); } try { MultiFormatWriter writer = new MultiFormatWriter(); //构造创建参数; Hashtable hints = new Hashtable(); hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.Add(EncodeHintType.ERROR_CORRECTION, ecLevel); Bitmap QRImg = writer.encode(info, BarcodeFormat.QR_CODE, width, height, hints).ToBitmap(); //生成不带图片的BitMap; Image midImg = null; if (!string.IsNullOrEmpty(midImgPath) && File.Exists(midImgPath) && legalExts.Contains(Path.GetExtension(midImgPath))) //验证图片存在且后缀名符合 { try //确保即使中间图片加载失败也能生成无中间图的QR码 { midImg = Image.FromFile(midImgPath); } catch { } } Bitmap bmpimg = new Bitmap(QRImg.Width, QRImg.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(QRImg, 0, 0); } if (midImg != null) { Size QRSize = writer.GetEncodeSize(info, BarcodeFormat.QR_CODE, width, height); //计算中间图片大小和位置 int midImgW = Math.Min((int)(QRSize.Width * midImgScale), midImg.Width); int midImgH = Math.Min((int)(QRSize.Height * midImgScale), midImg.Height); int midImgL = (QRImg.Width - midImgW) / 2; int midImgT = (QRImg.Height - midImgH) / 2; Graphics myGraphic = System.Drawing.Graphics.FromImage(bmpimg); myGraphic.FillRectangle(Brushes.White, midImgL, midImgT, midImgW, midImgH); myGraphic.DrawImage(midImg, midImgL, midImgT, midImgW, midImgH); myGraphic.Dispose(); midImg.Dispose(); } if (!Directory.Exists(saveDir)) { Directory.CreateDirectory(saveDir); } QRImgPath = Path.Combine(saveDir, fileName + ".jpg"); bmpimg.Save(QRImgPath, ImageFormat.Jpeg); } catch (Exception e) { } return(QRImgPath); }
public static Texture2D EncodeQRcodeImageWithIcon(string content, int width, int height, Texture2D icon, TextureFormat textureFormat = TextureFormat.ARGB32, bool isSave = false, string savePath = "") { Texture2D texture2D = null; BitMatrix bitMatrix = null; try { MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); Dictionary <EncodeHintType, object> hits = new Dictionary <EncodeHintType, object>(); hits.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hits.Add(EncodeHintType.MARGIN, 1); hits.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H); bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hits); } catch (Exception e) { Debuger.LogError("Encode QRCode Image With Icon Error " + e.Message); return(null); } texture2D = new Texture2D(width, height, textureFormat, false); int halfWidth = texture2D.width / 2; int halfHeight = texture2D.height / 2; int halfWidthOfIcon = icon.width / 2; int halfHeightOfIcon = icon.height / 2; for (int i = 0; i < bitMatrix.Height; i++) { for (int j = 0; j < bitMatrix.Width; j++) { var centerOffsetX = i - halfWidth; var centerOffsetY = j - halfHeight; if (Mathf.Abs(centerOffsetX) <= halfWidthOfIcon && Mathf.Abs(centerOffsetY) <= halfHeightOfIcon) { texture2D.SetPixel(i, j, icon.GetPixel(centerOffsetX + halfWidthOfIcon, centerOffsetY + halfHeightOfIcon)); } else { if (bitMatrix[i, j]) { texture2D.SetPixel(i, j, Color.black); } else { texture2D.SetPixel(i, j, Color.white); } } } } texture2D.Apply(); if (isSave) { SaveQRcodeImage(texture2D, savePath); } return(texture2D); }
/// <summary> /// 生成中间带有图片的二维码图片 /// </summary> /// <param name="contents">要生成二维码包含的信息</param> /// <param name="middleImg">要生成到二维码中间的图片(30x30)</param> /// <param name="margin">白边宽度(0-4)</param> /// <param name="foreColor">前景色</param> /// <param name="width">生成的二维码宽度(默认300像素)</param> /// <param name="height">生成的二维码高度(默认300像素)</param> /// <returns>中间带有图片的二维码</returns> public static Bitmap GeneratorQrImage(string contents, Image middleImg, Color foreColor, int margin, int width = 300, int height = 300) { if (string.IsNullOrEmpty(contents)) { return(null); } if (middleImg == null) { //return null; return(GeneratorQrImage(contents)); } //构造二维码写码器 MultiFormatWriter mutiWriter = new MultiFormatWriter(); Dictionary <EncodeHintType, object> hint = new Dictionary <EncodeHintType, object>(); hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); // 调整二维码的容错参数ErrorCorrectionLevel 用于嵌入图片 hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hint.Add(EncodeHintType.MARGIN, margin); //生成二维码 BitMatrix bm = mutiWriter.encode(contents, BarcodeFormat.QR_CODE, width, height, hint); BarcodeWriter barcodeWriter = new BarcodeWriter(); barcodeWriter.Renderer = new ZXing.Rendering.BitmapRenderer { Foreground = foreColor }; Bitmap bitmap = barcodeWriter.Write(bm); //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) int[] rectangle = bm.getEnclosingRectangle(); //计算插入图片的大小和位置 int middleImgW = Math.Min((int)(rectangle[2] / 3.5), middleImg.Width); int middleImgH = Math.Min((int)(rectangle[3] / 3.5), middleImg.Height); int middleImgL = (bitmap.Width - middleImgW) / 2; int middleImgT = (bitmap.Height - middleImgH) / 2; //将img转换成bmp格式,否则后面无法创建 Graphics对象 Bitmap bmpimg = new Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(bitmap, 0, 0); } //在二维码中插入图片 Graphics myGraphic = Graphics.FromImage(bmpimg); //白底 myGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH); myGraphic.DrawImage(middleImg, middleImgL, middleImgT, middleImgW, middleImgH); return(bmpimg); }
//public int Delete(string masterId) //{ // int result = proxy.Delete(masterId); // return result; //} /// <summary> /// 生成带Logo的二维码 /// </summary> /// <param name="text">内容</param> /// <param name="width">宽度</param> /// <param name="height">高度</param> public static Bitmap Generate3(string logoPath, string text, int width, int height, string addLogo, Color BackC, Color ForeC) { //构造二维码写码器 MultiFormatWriter writer = new MultiFormatWriter(); Dictionary <EncodeHintType, object> hint = new Dictionary <EncodeHintType, object>(); hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //hint.Add(EncodeHintType.MARGIN, 2);//旧版本不起作用,需要手动去除白边 //生成二维码 BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width + 30, height + 30, hint); bm = deleteWhite(bm); BarcodeWriter barcodeWriter = new BarcodeWriter(); //设置二维码颜色 barcodeWriter.Renderer = new ZXing.Rendering.BitmapRenderer { Background = BackC, Foreground = ForeC }; Bitmap map = barcodeWriter.Write(bm); if (addLogo != "1") { return(map); } //Logo 图片 //string logoPath = System.AppDomain.CurrentDomain.BaseDirectory + @"\img\logo.png"; if (!System.IO.File.Exists(logoPath)) { return(map); } Bitmap logo = new Bitmap(logoPath); //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) int[] rectangle = bm.getEnclosingRectangle(); //计算插入图片的大小和位置 int middleW = Math.Min((int)(rectangle[2] / 3), logo.Width); int middleH = Math.Min((int)(rectangle[3] / 3), logo.Height); int middleL = (map.Width - middleW) / 2; int middleT = (map.Height - middleH) / 2; Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb); //return bmpimg; using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(map, 0, 0, map.Width, map.Height); //白底将二维码插入图片 //g.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH); g.FillRectangle(new SolidBrush(ForeC), middleL, middleT, middleW, middleH); g.DrawImage(logo, middleL, middleT, middleW, middleH); } return(bmpimg); }
/// <summary> /// 产生条码图片 /// </summary> /// <param name="barCode"></param> public Bitmap CreateBarCode128(string barCode, int width, int height) { MultiFormatWriter mutiWriter = new MultiFormatWriter(); BitMatrix bm = mutiWriter.encode(barCode, BarcodeFormat.CODE_128, width, height); Bitmap imageBarcode = new BarcodeWriter().Write(bm); return(imageBarcode); }
/// <summary> /// 生成带Logo的二维码 /// </summary> /// <param name="text"></param> public static string GenerateLogoQrCode(string text, string virtualPath) { //Logo 图片 Bitmap logo = new Bitmap(sys.Util.Config.GetValue("QRCodDefaultIconPath")); //构造二维码写码器 MultiFormatWriter writer = new MultiFormatWriter(); Dictionary <EncodeHintType, object> hint = new Dictionary <EncodeHintType, object>(); hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //生成二维码 BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, QrCodeWidth, QrCodeHeight, hint); BarcodeWriter barcodeWriter = new BarcodeWriter(); Bitmap map = barcodeWriter.Write(bm); //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) int[] rectangle = bm.getEnclosingRectangle(); //计算插入图片的大小和位置 int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width); int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height); int middleL = (map.Width - middleW) / 2; int middleT = (map.Height - middleH) / 2; //将img转换成bmp格式,否则后面无法创建Graphics对象 Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(map, 0, 0); } //将二维码插入图片 Graphics myGraphic = Graphics.FromImage(bmpimg); //白底 myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH); myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH); //如果不存在就创建file文件夹 if (System.IO.Directory.Exists(virtualPath) == false)//如果不存在就创建file文件夹 { System.IO.Directory.CreateDirectory(virtualPath); } var NowTime = DateTime.Now.ToString("yyyyMMddHHmmss"); Random rNum = new Random(); string rNumStr = rNum.Next(1000, 10000).ToString(); string filename = string.Format("{0}{1}_{2}.png", virtualPath, NowTime, rNumStr); //保存成图片 bmpimg.Save(filename, ImageFormat.Png); map.Dispose(); bmpimg.Dispose(); return(string.Format("{0}_{1}.png", NowTime, rNumStr)); }
//带LOGO二维码 private void btnEncodePic_Click(object sender, EventArgs e) { labShow.Text = ""; string content = txtMsg.Text.Trim(); if (String.IsNullOrEmpty(content)) { MessageBox.Show("请输入编码内容"); return; } try { //构造二维码编码器 MultiFormatWriter writer = new MultiFormatWriter(); Hashtable hints = new Hashtable(); hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q); //先生成二维码 ByteMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, 300, 300, hints); Bitmap img = matrix.ToBitmap(); //再处理要插入到二维码中的图片 Image middlImg = QRMiddleImg.Image; //获取二维码实际尺寸(去掉二维码两边空白后的尺寸) Size realSize = writer.GetEncodeSize(content, BarcodeFormat.QR_CODE, 300, 300); //计算插入的图片的大小和位置 int middleImgW = Math.Min((int)(realSize.Width / 4), middlImg.Width); int middleImgH = Math.Min((int)(realSize.Height / 4), middlImg.Height); int middleImgL = (img.Width - middleImgW) / 2; int middleImgT = (img.Height - middleImgH) / 2; //将img转换成bmp格式,否则后面无法创建 Graphics对象 Bitmap bmpimg = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(img, 0, 0); } //在二维码中插入图片 Graphics MyGraphic = Graphics.FromImage(bmpimg); //白底 MyGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH); MyGraphic.DrawImage(middlImg, middleImgL, middleImgT, middleImgW, middleImgH); MyGraphic.DrawString(content, this.Font, new SolidBrush(Color.Black), middleImgW * 2 + 20, 300 - middleImgH / 2); pictureBox1.Image = bmpimg; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 显示绘制的二维码,任意尺寸正方形 /// </summary> /// <param name="_str">扫码信息</param> Sprite ShowQRCode(string _str, int _width, int _height, Texture2D centerIcon) { MultiFormatWriter writer = new MultiFormatWriter(); Dictionary <EncodeHintType, object> hints = new Dictionary <EncodeHintType, object>() { { EncodeHintType.CHARACTER_SET, "UTF-8" }, { EncodeHintType.MARGIN, 1 }, { EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.M }, }; BitMatrix bitMatrix = writer.encode(_str, BarcodeFormat.QR_CODE, _width, _height, hints); int w = bitMatrix.Width; int h = bitMatrix.Height; Texture2D texture = new Texture2D(_width, _height); for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { if (bitMatrix[x, y]) { texture.SetPixel(y, x, Color.black); } else { texture.SetPixel(y, x, Color.white); } } } if (centerIcon != null) { // 添加小图 int halfWidth = texture.width / 2; int halfHeight = texture.height / 2; int halfWidthOfIcon = centerIcon.width / 2; int halfHeightOfIcon = centerIcon.height / 2; int centerOffsetX = 0; int centerOffsetY = 0; for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { centerOffsetX = x - halfWidth; centerOffsetY = y - halfHeight; if (Mathf.Abs(centerOffsetX) <= halfWidthOfIcon && Mathf.Abs(centerOffsetY) <= halfHeightOfIcon) { texture.SetPixel(x, y, centerIcon.GetPixel(centerOffsetX + halfWidthOfIcon, centerOffsetY + halfHeightOfIcon)); } } } } texture.Apply(); //创景精灵返回 return(Sprite.Create(texture, new Rect(0, 0, _width, _height), new Vector2(0.5f, 0.5f))); }
/// <summary> /// 产生条码图片 /// </summary> /// <param name="barCode"></param> public Bitmap CreateCode(string codeStr, int width, int height, BarcodeFormat codeFormat) { // Writer writer = new QRCodeWriter(); MultiFormatWriter mutiWriter = new MultiFormatWriter(); BitMatrix bm = mutiWriter.encode(codeStr, codeFormat, width, height); Bitmap imageBarcode = new BarcodeWriter().Write(bm); imageBarcode = GetBlackRGB(imageBarcode); return(imageBarcode); }
/// <summary> /// 生成二维码 /// </summary> /// <param name="content">二维码的内容</param> /// <param name="logo">二维码图标,如果没有则不会生成</param> /// <param name="codeMargin">二维码边距</param> /// <param name="codeWidth">二维码宽度</param> /// <param name="codeHeight">二维码长度</param> /// <returns></returns> public static Bitmap GenerateQrCode(string content, Bitmap logo, int codeMargin = 1, int codeWidth = 250, int codeHeight = 250) { //声明二维码生成器 MultiFormatWriter writer = new MultiFormatWriter(); //设置生成器的部分参数 Dictionary <EncodeHintType, object> hints = new Dictionary <EncodeHintType, object>() { { EncodeHintType.CHARACTER_SET, "UTF-8" }, //定义二维码字符串编码为UTF-8 { EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H }, //好像是设置生成器的误差值 { EncodeHintType.MARGIN, codeMargin }, //设置二维码边距 { EncodeHintType.DISABLE_ECI, true }, //只有使用ISO-8859-1规范才可以不禁用ECI }; //生成二维码 BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, codeWidth, codeHeight, hints); //声明二维码写入器,将二维码写入到bitmap里 BarcodeWriter barWriter = new BarcodeWriter(); Bitmap result = barWriter.Write(matrix);//如果没有特殊参数声明,直接用barWriter.Write(content)好像也是可以的 //插入Logo if (logo != null) { //计算二维码尺寸 // 第0位:左边距 // 第1位:上边距 // 第2位:二维码宽 // 第3位:二维码高 //获取二维码矩形区域 int[] rectangle = matrix.getEnclosingRectangle(); int logoWidth = Math.Min((int)(rectangle[2] / 3), logo.Width); //计算logo宽度,并且限制logo最小宽度不能小于二维码宽度的1/3 int logoHeight = Math.Min((int)(rectangle[3] / 3), logo.Height); //计算logo高度,并且限制logo最小高度不能小于二维码高度的1/3 int logoLeft = result.Width - logoWidth; //计算logo的左边距 int logoTop = result.Height - logoHeight; //计算logo的上边距 //新建一个bitmap用于合并二维码和logo Bitmap qrCode = new Bitmap(result.Width, result.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); //声明一个GDI+绘图类用来绘制二维码 using (Graphics graphics = Graphics.FromImage(qrCode)) { //设置图片的清晰度 graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //高清晰度 //设置图片抗锯齿 graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //高品质的抗锯齿 //把二维码画上去作为底色 graphics.DrawImage(result, 0, 0, codeWidth, codeHeight); //左边距,上边距为0 //在二维码上面再覆盖一层logo //绘制logo的矩形区域 graphics.FillRectangle(Brushes.White, logoLeft, logoTop, logoWidth, logoHeight); //矩形填充色为白色,既logo的背景色是白色。 //绘制logo graphics.DrawImage(logo, logoLeft, logoTop, logoWidth, logoHeight); } //覆盖原没有logo的二维码 result = qrCode; } return(result); }
/// <summary> /// /// </summary> /// <param name="content"></param> /// <param name="qrcodeImgPath"></param> /// <param name="wh"></param> /// <param name="centerImg"></param> /// <param name="color"></param> /// <param name="bgcolor"></param> /// <returns></returns> public static string QrCodeImg(string content, string qrcodeImgPath, int wh, string centerImg, string color, string bgcolor) { Color foregroundcolor = (color == null || color == "") ? ColorTranslator.FromHtml("#000000") : ColorTranslator.FromHtml(color); Color backgroundcolor = (color == null || color == "") ? Color.Transparent : ColorTranslator.FromHtml(bgcolor); //Logo 图片 Bitmap logo = new Bitmap(centerImg); //构造二维码写码器 MultiFormatWriter writer = new MultiFormatWriter(); Dictionary <EncodeHintType, object> hint = new Dictionary <EncodeHintType, object>(); hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hint.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H); //生成二维码 BitMatrix bm = writer.encode(content, BarcodeFormat.QR_CODE, 300, 300, hint); BarcodeWriter barcodeWriter = new BarcodeWriter(); barcodeWriter.Renderer = new ZXing.Rendering.BitmapRenderer { Background = backgroundcolor, Foreground = foregroundcolor }; //设置颜色 Bitmap map = barcodeWriter.Write(bm); //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) int[] rectangle = bm.getEnclosingRectangle(); //计算插入图片的大小和位置 int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width); int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height); int middleL = (map.Width - middleW) / 2; int middleT = (map.Height - middleH) / 2; //将img转换成bmp格式,否则后面无法创建Graphics对象 Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(map, 0, 0); } //将二维码插入图片 Graphics myGraphic = Graphics.FromImage(bmpimg); //白底 myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH); myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH); //保存成图片 bmpimg.Save(qrcodeImgPath, ImageFormat.Png); bmpimg.Dispose(); return(qrcodeImgPath); }
public static Bitmap createQRCode(String strCode, int widthAndHeight) { MultiFormatWriter mfwr = new MultiFormatWriter(); ByteMatrix matrix = mfwr.encode(strCode, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight); if (matrix != null) { Bitmap bitmap = matrix.ToBitmap(); return(bitmap); } return(null); }
public static string GenerateWithLogo(string path, string str, string logopath, string imgpath) { var logo_path = DowloadImgByUrl(logopath, imgpath); if (logo_path != string.Empty) { Bitmap logo = new Bitmap(logopath); //初始化 MultiFormatWriter writer = new MultiFormatWriter(); Dictionary <EncodeHintType, object> hint = new Dictionary <EncodeHintType, object>(); hint.Add(EncodeHintType.CHARACTER_SET, str); hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //生成二维码 BitMatrix bm = writer.encode(str, BarcodeFormat.QR_CODE, 150, 150, hint); BarcodeWriter barcodeWriter = new BarcodeWriter(); Bitmap map = barcodeWriter.Write(bm); //计算尺寸 int[] rectangle = bm.getEnclosingRectangle(); if (!string.IsNullOrEmpty(logopath)) { } //计算插入Logo的大小位置 int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width); int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height); int middleL = (map.Width - middleW) / 2; int middleT = (map.Height - middleH) / 2; //将img转换成bmp格式,否则后面无法创建Graphics对象 Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(map, 0, 0); } //将二维码插入图片 Graphics myGraphic = Graphics.FromImage(bmpimg); //白底 myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH); myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH); //保存成图片 bmpimg.Save(path, System.Drawing.Imaging.ImageFormat.Png); return(path); } else { return(Generate2Code(path, str)); } }
/// <summary> /// 生成中心带有小图标二维码 /// </summary> /// <param name="content"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="centerIcon"></param> /// <returns></returns> public Texture2D GenerateQRImageWithIcon(string content, int width, int height, Texture2D centerIcon) { // 编码成color32 MultiFormatWriter writer = new MultiFormatWriter(); Dictionary <EncodeHintType, object> hints = new Dictionary <EncodeHintType, object>(); hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.Add(EncodeHintType.MARGIN, 1); hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H); BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints); // 转成texture2d int w = bitMatrix.Width; int h = bitMatrix.Height; Texture2D texture = new Texture2D(w, h); for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { if (bitMatrix[x, y]) { texture.SetPixel(y, x, Color.black); } else { texture.SetPixel(y, x, Color.white); } } } // 添加小图 int halfWidth = texture.width / 2; int halfHeight = texture.height / 2; int halfWidthOfIcon = centerIcon.width / 2; int halfHeightOfIcon = centerIcon.height / 2; int centerOffsetX = 0; int centerOffsetY = 0; for (int x = 0; x < h; x++) { for (int y = 0; y < w; y++) { centerOffsetX = x - halfWidth; centerOffsetY = y - halfHeight; if (Mathf.Abs(centerOffsetX) <= halfWidthOfIcon && Mathf.Abs(centerOffsetY) <= halfHeightOfIcon) { texture.SetPixel(x, y, centerIcon.GetPixel(centerOffsetX + halfWidthOfIcon, centerOffsetY + halfHeightOfIcon)); } } } texture.Apply(); return(texture); }
private void button1_Click(object sender, EventArgs e) { MultiFormatWriter mutiWriter = new MultiFormatWriter(); BitMatrix bm = mutiWriter.encode("http://www.vgo2013.com/WebService/QR.aspx?ID=" + "111111", BarcodeFormat.QR_CODE, 300, 300); ZXing.Rendering.BitmapRenderer br = new ZXing.Rendering.BitmapRenderer(); Bitmap img = br.Render(bm, BarcodeFormat.QR_CODE, "http://www.vgo2013.com/WebService/QR.aspx?ID=" + "111111"); img = KiResizeImage(img, 90, 90, 0); string filename = System.Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString() + ".jpg"; img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg); }
/// <summary> /// 生成带Logo的二维码 /// </summary> /// <param name="_text">文本内容</param> /// <param name="_logoPath">Logo路径</param> /// <param name="_codeSize">二维码大小</param> /// <param name="_codeMargin">边框大小</param> /// <returns></returns> public static Bitmap GenWithLogo(string _text, string _logoPath, int _codeSize, int _codeMargin, Color _colorBack, Color _colorFront) { //Logo 图片 Bitmap logo = new Bitmap(_logoPath); //构造二维码写码器 MultiFormatWriter writer = new MultiFormatWriter(); Dictionary <EncodeHintType, object> hint = new Dictionary <EncodeHintType, object>(); hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //生成二维码 BitMatrix bm = writer.encode(_text, BarcodeFormat.QR_CODE, _codeSize, _codeSize, hint); BarcodeWriter barcodeWriter = new BarcodeWriter(); barcodeWriter.Options.Margin = _codeMargin; barcodeWriter.Renderer = new ZXing.Rendering.BitmapRenderer { Background = _colorBack, Foreground = _colorFront }; Bitmap map = barcodeWriter.Write(bm); //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) int[] rectangle = bm.getEnclosingRectangle(); //计算插入图片的大小和位置 int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width); int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height); int middleL = (map.Width - middleW) / 2; int middleT = (map.Height - middleH) / 2; //将img转换成bmp格式,否则后面无法创建Graphics对象 Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(map, 0, 0); } //将二维码插入图片 Graphics myGraphic = Graphics.FromImage(bmpimg); //白底 myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH); myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH); return(bmpimg); }
/// <summary> /// 创建二维码 /// </summary> /// <param name="msg">二维码中保存的信息</param> /// <returns></returns> public static Bitmap Create(string msg) { var writer = new MultiFormatWriter(); var hint = new Dictionary <EncodeHintType, object>(); //设置二维码为utf-8编码 hint.Add(EncodeHintType.CHARACTER_SET, "utf-8"); //设置纠错等级, 高 hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); var bm = writer.encode(msg, BarcodeFormat.QR_CODE, 200, 200, hint); var barcodeWriter = new BarcodeWriter(); return(barcodeWriter.Write(bm)); }
/// <summary> /// 创建带图片的二维码 /// </summary> /// <param name="message">信息</param> /// <param name="middleImg">图片</param> /// <param name="pixel">大小</param> /// <returns></returns> private Bitmap CreateQrCode(string message, string imagePath, int pixel) { using (Image middleImg = new Bitmap(imagePath)) { //构造二维码写码器 MultiFormatWriter mutiWriter = new MultiFormatWriter(); Dictionary <EncodeHintType, object> encodeHint = new Dictionary <EncodeHintType, object>(); encodeHint.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); encodeHint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //生成二维码 BitMatrix bitMatrix = mutiWriter.encode(message, BarcodeFormat.QR_CODE, pixel, pixel, encodeHint); ZKWebNet.BarcodeWriter barcodeWriter = new ZKWebNet.BarcodeWriter(); Bitmap bitmap = barcodeWriter.Write(bitMatrix); //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) int[] rectangle = bitMatrix.getEnclosingRectangle(); //计算插入图片的大小和位置 int middleImgW = Math.Min((int)(rectangle[2] / 3.5), middleImg.Width); int middleImgH = Math.Min((int)(rectangle[3] / 3.5), middleImg.Height); int middleImgL = (bitmap.Width - middleImgW) / 2; int middleImgT = (bitmap.Height - middleImgH) / 2; //将img转换成bmp格式,否则后面无法创建 Graphics对象 Bitmap bmpimg = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb); using (Graphics graphics = Graphics.FromImage(bmpimg)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.DrawImage(bitmap, 0, 0); } //在二维码中插入图片 using (Graphics myGraphic = Graphics.FromImage(bmpimg)) { //白底 myGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH); myGraphic.DrawImage(middleImg, middleImgL, middleImgT, middleImgW, middleImgH); } bitmap.Dispose(); return(bmpimg); } }
/// <summary> /// 生成二维码并添加Logo /// 注:自定义边长以及颜色 /// </summary> /// <param name="content">二维码内容</param> /// <param name="imgSize">二维码边长px</param> /// <param name="background">二维码底色</param> /// <param name="foreground">二维码前景色</param> /// <param name="logo">logo图片</param> /// <returns></returns> public static Image BuildQRCode_Logo(string content, int imgSize, Color background, Color foreground, Image logo) { //构造二维码写码器 MultiFormatWriter writer = new MultiFormatWriter(); Dictionary <EncodeHintType, object> hint = new Dictionary <EncodeHintType, object> { { EncodeHintType.CHARACTER_SET, "UTF-8" }, { EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H } }; //生成二维码 BitMatrix bm = writer.encode(content, BarcodeFormat.QR_CODE, imgSize, imgSize, hint); BarcodeWriter barcodeWriter = new BarcodeWriter() { Renderer = new BitmapRenderer { Background = background, Foreground = foreground }, Format = BarcodeFormat.QR_CODE }; Bitmap map = barcodeWriter.Write(bm); //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) int[] rectangle = bm.getEnclosingRectangle(); //计算插入图片的大小和位置 int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width); int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height); int middleL = (map.Width - middleW) / 2; int middleT = (map.Height - middleH) / 2; //将img转换成bmp格式,否则后面无法创建Graphics对象 Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(map, 0, 0); } //将二维码插入图片 Graphics myGraphic = Graphics.FromImage(bmpimg); //白底 myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH); myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH); return(bmpimg); }