图片合成YFFotoMix

举报
清雨小竹 发表于 2022/09/25 01:58:56 2022/09/25
【摘要】 using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Text;using System.IO;using System.Linq;using System.Net;using System.Web;using YF...

  
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Text;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Web;
  9. using YFAPICommon.Controllers;
  10. namespace YFAPICommon.Libs
  11. {
  12. public class YFFotoMix
  13. {
  14. private static string serverPath = System.Configuration.ConfigurationSettings.AppSettings["serverPath"];
  15. private static string localPath = System.Configuration.ConfigurationSettings.AppSettings["localPath"];
  16. /// <summary>
  17. /// 合成用户分享图片
  18. /// </summary>
  19. /// <returns></returns>
  20. public static string CombinUserShareImg(string str,string imgUrl,string wxPath,string fileName)
  21. {
  22. const string folderPath = "\\shareimg\\";
  23. const string serverFolderPath = "/shareimg/";
  24. string folder = localPath + folderPath;
  25. //判断文件的存在
  26. if (System.IO.File.Exists(Path.Combine(folder, fileName+".jpg")))
  27. {
  28. //存在文件
  29. return serverPath + serverFolderPath + fileName + ".jpg";
  30. }
  31. Image img1 = Image.FromFile(Path.Combine(folder, "sharetemp1.jpg"));//相框图片
  32. Image img2 = DrawTransparentRoundCornerImage(downloadImage(imgUrl),30); //照片图片
  33. Image wxQrCode = DrawTransparentRoundCornerImage(WXController.getWXCodeImage(wxPath));
  34. //从指定的System.Drawing.Image创建新的System.Drawing.Graphics
  35. Graphics g = Graphics.FromImage(img1);
  36. g.DrawImage(img1, 0, 0, img1.Size.Width, img1.Size.Height);// g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);
  37. //g.FillRectangle(System.Drawing.Brushes.Black, 16, 16, (int)112 + 2, ((int)73 + 2));//相片四周刷一层黑色边框
  38. //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
  39. g.DrawImage(img2, 75, 243, 650, 838);//户型图
  40. g.DrawImage(wxQrCode, 540, 1175, 200, 200);//小程序二维码
  41. //************************************
  42. g.TextRenderingHint = TextRenderingHint.AntiAlias;
  43. FontFamily fontFamily = new FontFamily("黑体");
  44. Font font1 = new Font(fontFamily, 28f, FontStyle.Regular, GraphicsUnit.Pixel);
  45. string poem1 = str;
  46. g.DrawString(poem1, font1, Brushes.White, 70, 1320);
  47. //************************************
  48. GC.Collect();
  49. img1.Save(Path.Combine(folder, fileName + ".jpg"));
  50. img1.Dispose();
  51. return serverPath+ serverFolderPath+ fileName + ".jpg";
  52. }
  53. public static string CombinUserShareImg2(string wxPath, string fileName)
  54. {
  55. const string folderPath = "\\shareimg\\";
  56. const string serverFolderPath = "/shareimg/";
  57. string folder = localPath + folderPath;
  58. //判断文件的存在
  59. if (System.IO.File.Exists(Path.Combine(folder, fileName + ".jpg")))
  60. {
  61. //存在文件
  62. return serverPath + serverFolderPath + fileName + ".jpg";
  63. }
  64. Image img1 = Image.FromFile(Path.Combine(folder, "sharetemp2.jpg"));//相框图片
  65. Image wxQrCode = DrawTransparentRoundCornerImage(WXController.getWXCodeImage(wxPath));
  66. //从指定的System.Drawing.Image创建新的System.Drawing.Graphics
  67. Graphics g = Graphics.FromImage(img1);
  68. g.DrawImage(img1, 0, 0, img1.Size.Width, img1.Size.Height);// g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);
  69. //g.FillRectangle(System.Drawing.Brushes.Black, 16, 16, (int)112 + 2, ((int)73 + 2));//相片四周刷一层黑色边框
  70. //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
  71. g.DrawImage(wxQrCode, 590, 1225, 160, 160);//小程序二维码
  72. //************************************
  73. GC.Collect();
  74. img1.Save(Path.Combine(folder, fileName + ".jpg"));
  75. img1.Dispose();
  76. return serverPath + serverFolderPath + fileName + ".jpg";
  77. }
  78. //图片处理为圆角
  79. private static System.Drawing.Image DrawTransparentRoundCornerImage(System.Drawing.Image image,int radius)
  80. {
  81. Bitmap bm = new Bitmap(image.Width, image.Height);
  82. Graphics g = Graphics.FromImage(bm);
  83. g.FillRectangle(Brushes.Transparent, new Rectangle(0, 0, image.Width, image.Height));
  84. using (System.Drawing.Drawing2D.GraphicsPath path = CreateRoundedRectanglePath(new Rectangle(0, 0, image.Width, image.Height), radius))
  85. {
  86. g.SetClip(path);
  87. }
  88. g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
  89. g.Dispose();
  90. return bm;
  91. }
  92. //图片处理为圆形
  93. private static System.Drawing.Image DrawTransparentRoundCornerImage(System.Drawing.Image image)
  94. {
  95. Bitmap bm = new Bitmap(image.Width, image.Height);
  96. Graphics g = Graphics.FromImage(bm);
  97. g.FillRectangle(Brushes.Transparent, new Rectangle(0, 0, image.Width, image.Height));
  98. using (System.Drawing.Drawing2D.GraphicsPath path = CreateRoundedRectanglePath(new Rectangle(0, 0, image.Width, image.Height), image.Width / 2))
  99. {
  100. g.SetClip(path);
  101. }
  102. g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
  103. g.Dispose();
  104. return bm;
  105. }
  106. //设置图片四个边角弧度
  107. private static System.Drawing.Drawing2D.GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
  108. {
  109. System.Drawing.Drawing2D.GraphicsPath roundedRect = new System.Drawing.Drawing2D.GraphicsPath();
  110. roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
  111. roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
  112. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
  113. roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
  114. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
  115. roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
  116. roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
  117. roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
  118. roundedRect.CloseFigure();
  119. return roundedRect;
  120. }
  121. private static Image downloadImage(string url)
  122. {
  123. WebRequest imgRequest = WebRequest.Create(url);
  124. HttpWebResponse res;
  125. try
  126. {
  127. res = (HttpWebResponse)imgRequest.GetResponse();
  128. }
  129. catch (WebException ex)
  130. {
  131. res = (HttpWebResponse)ex.Response;
  132. }
  133. if (res.StatusCode.ToString() == "OK")
  134. {
  135. System.Drawing.Image downImage = System.Drawing.Image.FromStream(imgRequest.GetResponse().GetResponseStream());
  136. return downImage;
  137. }
  138. return null;
  139. }
  140. }
  141. }

 

文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。

原文链接:zzzili.blog.csdn.net/article/details/115328463

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。