微信建立帶參數二維碼,並加上logo

如今須要建立一個場景二維碼,除了基礎的微信接口建立外,須要加上小logo,思路以下:web

一、 首先根據微信的開發文檔建立二維碼,獲取二維碼的url,沒啥可說的,按照文檔來就行了微信

 獲取到的二維碼就是這麼素淨~微信開發

 

二、獲得了下載地址,咱們就已文件流的方式,將二維碼的流,轉換爲圖像對象,並將指定的圖片轉換爲圖像對象(注意:地址必須是絕對路徑         ide

      /// <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 + @"logo\logo3.png");             //合併 var ret = new ImageUtility().MergeQrImg(b1, b2, 1); Image img = ret; img.Save(AppDomain.CurrentDomain.BaseDirectory + urlPath); string path = urlPath;             //返回最終路徑 return path; } }

 

這個是合併圖片使用到的幫助類,本身領悟this

logo大小的調整、邊框顏色的調整在幫助類中能夠自行設置。url

  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         }
View Code

 
最後生成的圖片相似這樣:spa

 

微信開發文檔:https://mp.weixin.qq.com/wikicode

相關文章
相關標籤/搜索