现在需要创建一个场景二维码,除了基础的微信接口创建外,需要加上小logo,思路如下:
1、 首先根据微信的开发文档创建二维码,获取二维码的url,没啥可说的,按照文档来就好了
获取到的二维码就是这么素净~
2、得到了下载地址,我们就已文件流的方式,将二维码的流,转换为图像对象,并将指定的图片转换为图像对象(注意:地址必须是绝对路径
/// <summary>
/// 下载二维码图片 /// </summary> /// <param name="dirName">文件路径</param> /// <param name="fileName">文件名</param> /// <param name="downloadUrl">下载地址</param> /// <param name="url">最终图片存放地址</param> /// <returns></returns> private string LoadImg(string dirName,string fileName,string downloadUrl, out string url) { //设置文件保存的地址,格式,文件夹的判断和创建 string urlPath =CreateUrl(dirName,fileName, out url);// out 文件路径 HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(downloadUrl); req.Method = "GET"; using (WebResponse wr = req.GetResponse()) { HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse(); string strpath = myResponse.ResponseUri.ToString(); WebClient mywebclient = new WebClient(); //开始了 //素净的二维码 byte[] bytelist = mywebclient.DownloadData(strpath); MemoryStream ms1 = new MemoryStream(bytelist); Bitmap b1 = (Bitmap)Image.FromStream(ms1); ms1.Close(); //logo图片 Bitmap b2 = new Bitmap(AppDomain.CurrentDomain.BaseDirectory + @"logologo3.png"); //合并 var ret = new ImageUtility().MergeQrImg(b1, b2, 1); Image img = ret; img.Save(AppDomain.CurrentDomain.BaseDirectory + urlPath); string path = urlPath; //返回最终路径 return path; } }
这个是合并图片使用到的帮助类,自己领悟
logo大小的调整、边框颜色的调整在帮助类中可以自行设置。
1 public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidth) 2 { 3 System.Drawing.Image imgSource = b; 4 System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat; 5 int sW = 0, sH = 0; 6 // 按比例缩放 7 int sWidth = imgSource.Width; 8 int sHeight = imgSource.Height; 9 if (sHeight > destHeight || sWidth > destWidth) 10 { 11 if ((sWidth * destHeight) > (sHeight * destWidth)) 12 { 13 sW = destWidth; 14 sH = (destWidth * sHeight) / sWidth; 15 } 16 else 17 { 18 sH = destHeight; 19 sW = (sWidth * destHeight) / sHeight; 20 } 21 } 22 else 23 { 24 sW = sWidth; 25 sH = sHeight; 26 } 27 Bitmap outBmp = new Bitmap(destWidth, destHeight); 28 Graphics g = Graphics.FromImage(outBmp); 29 g.Clear(Color.Transparent); 30 // 设置画布的描绘质量 31 g.CompositingQuality = CompositingQuality.HighQuality; 32 g.SmoothingMode = SmoothingMode.HighQuality; 33 g.InterpolationMode = InterpolationMode.HighQualityBicubic; 34 g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel); 35 g.Dispose(); 36 // 以下代码为保存图片时,设置压缩质量 37 EncoderParameters encoderParams = new EncoderParameters(); 38 long[] quality = new long[1]; 39 quality[0] = 100; 40 EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); 41 encoderParams.Param[0] = encoderParam; 42 imgSource.Dispose(); 43 return outBmp; 44 } 45 } 46 47 public class ImageUtility 48 { 49 #region 合并用户QR图片和用户头像 50 /// <summary> 51 /// 合并用户QR图片和用户头像 52 /// </summary> 53 /// <param name="qrImg">QR图片</param> 54 /// <param name="headerImg">用户头像</param> 55 /// <param name="n">缩放比例</param> 56 /// <returns></returns> 57 public Bitmap MergeQrImg(Bitmap qrImg, Bitmap headerImg, double n = 0.23) 58 { 59 int margin = 10; 60 float dpix = qrImg.HorizontalResolution; 61 float dpiy = qrImg.VerticalResolution; 62 var _newWidth = (10 * qrImg.Width - 46 * margin) * 1.0f / 46; 63 var _headerImg = ZoomPic(headerImg, _newWidth / headerImg.Width); 64 //处理头像 65 int newImgWidth = _headerImg.Width + margin; 66 Bitmap headerBgImg = new Bitmap(newImgWidth, newImgWidth); 67 headerBgImg.MakeTransparent(); 68 Graphics g = Graphics.FromImage(headerBgImg); 69 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 70 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 71 g.Clear(Color.Transparent); 72 Pen p = new Pen(new SolidBrush(Color.White)); 73 //位置和大小 74 Rectangle rect = new Rectangle(0, 0, newImgWidth - 1, newImgWidth - 1); 75 using (GraphicsPath path = CreateRoundedRectanglePath(rect, 7)) 76 { 77 g.DrawPath(p, path); 78 g.FillPath(new SolidBrush(Color.White), path); 79 } 80 //画头像 81 Bitmap img1 = new Bitmap(_headerImg.Width, _headerImg.Width); 82 Graphics g1 = Graphics.FromImage(img1); 83 g1.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 84 g1.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 85 g1.Clear(Color.Transparent); 86 //Pen p1 = new Pen(new SolidBrush(Color.Gray)); 87 Pen p1 = new Pen(new SolidBrush(Color.White)); 88 Rectangle rect1 = new Rectangle(0, 0, _headerImg.Width - 1, _headerImg.Width - 1); 89 using (GraphicsPath path1 = CreateRoundedRectanglePath(rect1, 8)) 90 { 91 g1.DrawPath(p1, path1); 92 TextureBrush brush = new TextureBrush(_headerImg); 93 g1.FillPath(brush, path1); 94 } 95 g1.Dispose(); 96 PointF center = new PointF((newImgWidth - _headerImg.Width) / 2, (newImgWidth - _headerImg.Height) / 2); 97 g.DrawImage(img1, center.X, center.Y, _headerImg.Width, _headerImg.Height); 98 g.Dispose(); 99 Bitmap backgroudImg = new Bitmap(qrImg.Width, qrImg.Height); 100 backgroudImg.MakeTransparent(); 101 backgroudImg.SetResolution(dpix, dpiy); 102 headerBgImg.SetResolution(dpix, dpiy); 103 Graphics g2 = Graphics.FromImage(backgroudImg); 104 g2.Clear(Color.Transparent); 105 g2.DrawImage(qrImg, 0, 0); 106 PointF center2 = new PointF((qrImg.Width - headerBgImg.Width) / 2, (qrImg.Height - headerBgImg.Height) / 2); 107 g2.DrawImage(headerBgImg, center2); 108 g2.Dispose(); 109 return backgroudImg; 110 } 111 #endregion 112 113 #region 图形处理 114 /// <summary> 115 /// 创建圆角矩形 116 /// </summary> 117 /// <param name="rect">区域</param> 118 /// <param name="cornerRadius">圆角角度</param> 119 /// <returns></returns> 120 private GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius) 121 { 122 //下午重新整理下,圆角矩形 123 GraphicsPath roundedRect = new GraphicsPath(); 124 roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90); 125 roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y); 126 roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90); 127 roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2); 128 roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90); 129 roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom); 130 roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90); 131 roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2); 132 roundedRect.CloseFigure(); 133 return roundedRect; 134 } 135 /// <summary> 136 /// 图片按比例缩放 137 /// </summary> 138 private Image ZoomPic(Image initImage, double n) 139 { 140 //缩略图宽、高计算 141 double newWidth = initImage.Width; 142 double newHeight = initImage.Height; 143 newWidth = n * initImage.Width; 144 newHeight = n * initImage.Height; 145 //生成新图 146 //新建一个bmp图片 147 System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWidth, (int)newHeight); 148 //新建一个画板 149 System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage); 150 //设置质量 151 newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 152 newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 153 //置背景色 154 newG.Clear(Color.Transparent); 155 //画图 156 newG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel); 157 newG.Dispose(); 158 return newImage; 159 }
最后生成的图片类似这样: