1. 程式人生 > 實用技巧 >使用Graphics合成帶二維碼和頭像的分享圖(小程式分享、App分享)

使用Graphics合成帶二維碼和頭像的分享圖(小程式分享、App分享)

適用於微信小程式分享圖、app分享圖

//分享的海報背景圖片 使用本地圖片檔案  
string path = Server.MapPath("/Content/images/backimg.jpg");
Image imgSrc = Image.FromFile(path);   //將檔案載入成圖片格式

//二維碼圖片檔案  使用遠端圖片檔案  
var qr_url="http://xxxxxx:8001/Images/qrimg.jpg";
Image qrCodeImage= ReduceImage(qr_url, 122, 122);

//頭像檔案  使用遠端圖片檔案  
var headurl="http://xxxxxx:8001/Images/header.jpg
"; Image headImage= ReduceImage(headurl, 59, 59); //開始合成圖片 宣告畫圖工具 using (Graphics g = Graphics.FromImage(imgSrc)) { //畫推廣二維碼 宣告起始座標點 //從x軸30畫素處、y軸的40畫素處開始畫 int qr_x = 30,qr_y = 40; //指定二維碼在合成圖上顯示的座標和區域大小 Rectangle destRect = new Rectangle(qr_x, qr_y, qrCodeImage.Width, qrCodeImage.Height);
//在destRect宣告的區域內,開始繪製二維碼 Rectangle srcRect = new Rectangle(0, 0, qrCodeImage.Width, qrCodeImage.Height); //開始繪製 GraphicsUnit.Pixel 表示以畫素為單位 g.DrawImage(qrCodeImage, destRect, srcRect, GraphicsUnit.Pixel); //畫頭像 裁剪頭像(圓形) Image header = CutEllipse(titleImage, new Size(59
, 59)); int w = header.Width, h = header.Height; //圖片寬高 int x = 24, y = 982; //圖片座標 g.DrawImage(header, new Rectangle(x, y, w, h), new Rectangle(0, 0, w, h), GraphicsUnit.Pixel); //畫暱稱 思源黑體 CN 常規 字型大小18 粗體 Font font = new Font("Source Han Sans CN Regular", 18, FontStyle.Bold, GraphicsUnit.Pixel); Color fontColor = (Color)new ColorConverter().ConvertFromString("#999999"); g.DrawString("張三", font, new SolidBrush(fontColor), 100, 990); //座標點 x=100 y=990 //畫其他文字 思源黑體 CN 常規 var othertext = "邀請您看直播"; g.DrawString(othertext, font, new SolidBrush(fontColor), 101, 1014); //座標點 x=100 y=990 header.Dispose(); g.Dispose(); } //獲取系統編碼型別陣列,包含了jpeg,bmp,png,gif,tiff int quality = 50; //影象質量 1 - 100的範圍 ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo ici = null; foreach (ImageCodecInfo i in icis) { if (i.MimeType == "image/jpeg" || i.MimeType == "image/bmp" || i.MimeType == "image/png" || i.MimeType == "image/gif") { ici = i; } } EncoderParameters ep = new EncoderParameters(1); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); //將圖片轉換成二進位制流 MemoryStream ms = new MemoryStream();//讀取位元組流 imgSrc.Save(ms, ici, ep); var buffer = ms.GetBuffer(); //圖片流 ms.Dispose(); imgSrc.Dispose(); qrCodeImage.Dispose(); headImage.Dispose(); ep.Dispose();

ReduceImage 方法可以縮放圖片大小

 1         /// <summary>
 2         /// 縮小/放大圖片  
 3         /// </summary>
 4         /// <param name="url">圖片網路地址</param>
 5         /// <param name="toWidth">縮小/放大寬度</param>
 6         /// <param name="toHeight">縮小/放大高度</param>
 7         /// <returns></returns>
 8         private static Image ReduceImage(string url, int toWidth, int toHeight, string param = null)
 9         {
10             try
11             {
12                 Stream responseStream = null;
13                 if (param != null)   //post請求
14                 {
15                     var intResult = HttpHelper.HttpClientPost(url, param, out responseStream);
16                 }
17                 else
18                 {
19                     WebRequest request = WebRequest.Create(url);
20                     WebResponse response = request.GetResponse();
21                     responseStream = response.GetResponseStream();
22                 }
23                 if (responseStream == null)
24                 {
25                     return null;
26                 }
27 
28                 Image originalImage = Image.FromStream(responseStream);
29                 if (toWidth <= 0 && toHeight <= 0)  //返回原圖
30                 {
31                     return originalImage;
32                 }
33                 else if (toWidth > 0 && toHeight > 0)  ////給了寬*高 如果原始小,則返回原圖
34                 {
35                     if (originalImage.Width < toWidth && originalImage.Height < toHeight)
36                         return originalImage;
37                 }
38                 else if (toWidth <= 0 && toHeight > 0)  //給了高,根據高計算寬,得出正方形圖片
39                 {
40                     if (originalImage.Height < toHeight)
41                         return originalImage;
42                     toWidth = originalImage.Width * toHeight / originalImage.Height;
43                 }
44                 else if (toHeight <= 0 && toWidth > 0)  //給了寬,根據寬計算高,得出正方形圖片
45                 {
46                     if (originalImage.Width < toWidth)
47                         return originalImage;
48                     toHeight = originalImage.Height * toWidth / originalImage.Width;
49                 }
50                 Image toBitmap = new Bitmap(toWidth, toHeight);   //定義一個指定大小的畫布背景
51                 using (Graphics g = Graphics.FromImage(toBitmap))   //定義畫圖工具,使用畫圖工具載入畫布背景,開始在畫布上作圖
52                 {
53                     //圖片縮放時使用
54                     g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;  //高速度、低質量
55                     g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //影象縮放質量
56                     //生成圖片時使用
57                     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;  //圖片呈現質量,即消除鋸齒
58                     g.Clear(Color.Transparent);  //清除背景色,並設定顏色為透明 Color.Transparent
59                     g.DrawImage(originalImage,
60                                 new Rectangle(0, 0, toWidth, toHeight),
61                                 new Rectangle(0, 0, originalImage.Width, originalImage.Height),
62                                 GraphicsUnit.Pixel);
63                     originalImage.Dispose();
64                     responseStream.Dispose();
65                     g.Dispose();
66                     return toBitmap;
67                 }
68             }
69             catch (Exception ex)
70             {
71                 throw;
72             }
73         }

CutEllipse方法用來裁剪頭像

 1         /// <summary>
 2         /// 畫圓形頭像
 3         /// </summary>
 4         /// <param name="img">待裁剪的圖片</param>
 5         /// <param name="size">裁剪大小</param>
 6         /// <returns></returns>
 7         private static Image CutEllipse(Image img, Size size)
 8         {
 9             try
10             {
11                 var rec = new Rectangle(0, 0, size.Width, size.Height); //宣告位置和大小
12                 Bitmap bitmap = new Bitmap(size.Width, size.Height);  //宣告畫布並指定大小
13                 Pen p = new Pen(Color.Transparent);  //宣告透明色畫筆
14                 using (Graphics g = Graphics.FromImage(bitmap))
15                 {
16                     using (TextureBrush br = new TextureBrush(img, rec))  //宣告畫刷
17                     {
18                         g.SmoothingMode = SmoothingMode.HighQuality;  //使繪圖質量最高,即消除鋸齒
19                         g.InterpolationMode = InterpolationMode.HighQualityBicubic;
20                         g.CompositingQuality = CompositingQuality.HighQuality;
21 
22                         g.FillEllipse(br, rec);  //根據指定大小和位置填充圓形
23                         g.DrawEllipse(p, rec);   //畫圓形圖
24                         br.Dispose();
25                     }
26                     g.Dispose();
27                 }
28                 return bitmap;
29             }
30             catch (Exception ex)
31             {
32                 throw;
33             }
34         }

效果圖: