最近做了一个生成二维码的功能,分享给朋友们,包括自定义二维码的颜色,给二维码中间添加个logo,效果如下
首先,调用 GetQRCode ,参数传入二维码中的信息
其次,GetQRCodeHaveLogo,给二维码添加一个logo
最后,欢迎拍砖
二维码类,需要引用ThoughtWorks.QRCode.dll
/// <summary> /// 生成二维码图片 /// </summary> /// <param name="data"></param> /// <returns></returns> public static Image GetQRCode(string data) { //生成二维码 string qrEncoding = "Byte"; string Level = "M"; string txt_ver = "7"; string txt_size = "4"; ThoughtWorks.QRCode.Codec.QRCodeEncoder qrCodeEncoder = new ThoughtWorks.QRCode.Codec.QRCodeEncoder(); String encoding = qrEncoding; if (encoding == "Byte") { qrCodeEncoder.QRCodeEncodeMode = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ENCODE_MODE.BYTE; } else if (encoding == "AlphaNumeric") { qrCodeEncoder.QRCodeEncodeMode = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC; } else if (encoding == "Numeric") { qrCodeEncoder.QRCodeEncodeMode = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ENCODE_MODE.NUMERIC; } try { int scale = Convert.ToInt16(txt_size); qrCodeEncoder.QRCodeScale = scale; } catch (Exception ex) { return null; } try { int version = Convert.ToInt16(txt_ver); qrCodeEncoder.QRCodeVersion = version; } catch (Exception ex) { return null; } string errorCorrect = Level; if (errorCorrect == "L") qrCodeEncoder.QRCodeErrorCorrect = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.L; else if (errorCorrect == "M") qrCodeEncoder.QRCodeErrorCorrect = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.M; else if (errorCorrect == "Q") qrCodeEncoder.QRCodeErrorCorrect = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.Q; else if (errorCorrect == "H") qrCodeEncoder.QRCodeErrorCorrect = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.H; Image image; qrCodeEncoder.QRCodeForegroundColor = Color.FromArgb(242, 133, 77);//二维码颜色 image = qrCodeEncoder.Encode(data, System.Text.Encoding.UTF8); var width = image.Width / 10; var dwidth = width * 2; Bitmap bmp = new Bitmap(image.Width + dwidth, image.Height + dwidth); Graphics g = Graphics.FromImage(bmp); var c = Color.FromArgb(252, 131, 54); g.FillRectangle(new SolidBrush(c), 0, 0, image.Width + dwidth, image.Height + dwidth); g.DrawImage(image, width, width); g.Dispose(); image.Dispose(); return bmp; }
/// <summary> /// 生成带logo的二维码图片 /// </summary> /// <param name="imgBack">二维码图片</param> /// <param name="destImg">logo路径</param> /// <returns></returns> public static Image GetQRCodeHaveLogo(Image imgBack, string destImg) { Image img = Image.FromFile(destImg); if (img.Height != 65 || img.Width != 65) {
img = ChangeAlpha(img); img = KiResizeImage(img, 65, 65, 0); } Graphics g = Graphics.FromImage(imgBack); g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height); g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2, img.Width, img.Height); GC.Collect(); return imgBack; }
/// <summary> /// 截取指定大小的图片 /// </summary> private static Image KiResizeImage(Image bmp, int newW, int newH, int Mode) { try { Image b = new Bitmap(newW, newH); Graphics g = Graphics.FromImage(b); g.InterpolationMode = InterpolationMode.HighQualityBicubic; GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(new Rectangle(0, 0, newW, newH)); g.SetClip(gp); //设置为圆形图片 g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel); g.Dispose(); return b; } catch (Exception ex) { return null; } }
//改变图片的透明度 private static Image ChangeAlpha(Image image) { Bitmap img = new Bitmap(image); using (Bitmap bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { using (Graphics g = Graphics.FromImage(bmp)) { g.DrawImage(img, 0, 0); for (int h = 0; h <= img.Height - 1; h++) { for (int w = 0; w <= img.Width - 1; w++) { Color c = img.GetPixel(w, h); int pNum = 230; bmp.SetPixel(w, h, Color.FromArgb(pNum, c.R, c.G, c.B)); } } return (Image)bmp.Clone(); } } }