1. public class ImageUtility
  2. {
  3. #region 合并用户QR图片和用户头像
  4.  
  5. /// <summary>
  6. /// 合并用户QR图片和用户头像
  7. /// </summary>
  8. /// <param name="qrImg">QR图片</param>
  9. /// <param name="headerImg">用户头像</param>
  10. /// <param name="n"></param>
  11. /// <returns></returns>
  12. public Bitmap MergeQrImg(Bitmap qrImg, Bitmap headerImg, double n = 0.23)
  13. {
  14. int margin = ;
  15. float dpix = qrImg.HorizontalResolution;
  16. float dpiy = qrImg.VerticalResolution;
  17. var _newWidth = ( * qrImg.Width - * margin) * 1.0f / ;
  18. var _headerImg = ZoomPic(headerImg, _newWidth / headerImg.Width);
  19. //处理头像
  20. int newImgWidth = _headerImg.Width + margin;
  21. Bitmap headerBgImg = new Bitmap(newImgWidth, newImgWidth);
  22. headerBgImg.MakeTransparent();
  23. Graphics g = Graphics.FromImage(headerBgImg);
  24. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  25. g.SmoothingMode = SmoothingMode.HighQuality;
  26. g.Clear(Color.Transparent);
  27. Pen p = new Pen(new SolidBrush(Color.White));
  28. Rectangle rect = new Rectangle(, , newImgWidth - , newImgWidth - );
  29. using (GraphicsPath path = CreateRoundedRectanglePath(rect, ))
  30. {
  31. g.DrawPath(p, path);
  32. g.FillPath(new SolidBrush(Color.White), path);
  33. }
  34. //画头像
  35. Bitmap img1 = new Bitmap(_headerImg.Width, _headerImg.Width);
  36. Graphics g1 = Graphics.FromImage(img1);
  37. g1.InterpolationMode = InterpolationMode.HighQualityBicubic;
  38. g1.SmoothingMode = SmoothingMode.HighQuality;
  39. g1.Clear(Color.Transparent);
  40. Pen p1 = new Pen(new SolidBrush(Color.Gray));
  41. Rectangle rect1 = new Rectangle(, , _headerImg.Width - , _headerImg.Width - );
  42. using (GraphicsPath path1 = CreateRoundedRectanglePath(rect1, ))
  43. {
  44. g1.DrawPath(p1, path1);
  45. TextureBrush brush = new TextureBrush(_headerImg);
  46. g1.FillPath(brush, path1);
  47. }
  48. g1.Dispose();
  49. PointF center = new PointF((newImgWidth - _headerImg.Width) / , (newImgWidth - _headerImg.Height) / );
  50. g.DrawImage(img1, center.X, center.Y, _headerImg.Width, _headerImg.Height);
  51. g.Dispose();
  52. Bitmap backgroudImg = new Bitmap(qrImg.Width, qrImg.Height);
  53. backgroudImg.MakeTransparent();
  54. backgroudImg.SetResolution(dpix, dpiy);
  55. headerBgImg.SetResolution(dpix, dpiy);
  56. Graphics g2 = Graphics.FromImage(backgroudImg);
  57. g2.Clear(Color.Transparent);
  58. g2.DrawImage(qrImg, , );
  59. PointF center2 = new PointF((qrImg.Width - headerBgImg.Width) / , (qrImg.Height - headerBgImg.Height) / );
  60. g2.DrawImage(headerBgImg, center2);
  61. g2.Dispose();
  62. return backgroudImg;
  63. }
  64. #endregion
  65.  
  66. #region 图形处理
  67. /// <summary>
  68. /// 创建圆角矩形
  69. /// </summary>
  70. /// <param name="rect">区域</param>
  71. /// <param name="cornerRadius">圆角角度</param>
  72. /// <returns></returns>
  73. private GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
  74. {
  75. //下午重新整理下,圆角矩形
  76. GraphicsPath roundedRect = new GraphicsPath();
  77. roundedRect.AddArc(rect.X, rect.Y, cornerRadius * , cornerRadius * , , );
  78. roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * , rect.Y);
  79. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * , rect.Y, cornerRadius * , cornerRadius * , , );
  80. roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * , rect.Right, rect.Y + rect.Height - cornerRadius * );
  81. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * , rect.Y + rect.Height - cornerRadius * , cornerRadius * , cornerRadius * , , );
  82. roundedRect.AddLine(rect.Right - cornerRadius * , rect.Bottom, rect.X + cornerRadius * , rect.Bottom);
  83. roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * , cornerRadius * , cornerRadius * , , );
  84. roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * , rect.X, rect.Y + cornerRadius * );
  85. roundedRect.CloseFigure();
  86. return roundedRect;
  87. }
  88. /// <summary>
  89. /// 图片按比例缩放
  90. /// </summary>
  91. private Image ZoomPic(Image initImage, double n)
  92. {
  93. //缩略图宽、高计算
  94. double newWidth = initImage.Width;
  95. double newHeight = initImage.Height;
  96. newWidth = n * initImage.Width;
  97. newHeight = n * initImage.Height;
  98. //生成新图
  99. //新建一个bmp图片
  100. Image newImage = new Bitmap((int)newWidth, (int)newHeight);
  101. //新建一个画板
  102. Graphics newG = Graphics.FromImage(newImage);
  103. //设置质量
  104. newG.InterpolationMode = InterpolationMode.HighQualityBicubic;
  105. newG.SmoothingMode = SmoothingMode.HighQuality;
  106. //置背景色
  107. newG.Clear(Color.Transparent);
  108. //画图
  109. newG.DrawImage(initImage, new Rectangle(, , newImage.Width, newImage.Height), new Rectangle(, , initImage.Width, initImage.Height), GraphicsUnit.Pixel);
  110. newG.Dispose();
  111. return newImage;
  112. }
  113.  
  114. /// <summary>
  115. /// 创建缩略图
  116. /// </summary>
  117. /// <param name="b"></param>
  118. /// <param name="destHeight"></param>
  119. /// <param name="destWidth"></param>
  120. /// <returns></returns>
  121. public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidth)
  122. {
  123. Image imgSource = b;
  124. ImageFormat thisFormat = imgSource.RawFormat;
  125. int sW = , sH = ;
  126. // 按比例缩放
  127. int sWidth = imgSource.Width;
  128. int sHeight = imgSource.Height;
  129. if (sHeight > destHeight || sWidth > destWidth)
  130. {
  131. if ((sWidth * destHeight) > (sHeight * destWidth))
  132. {
  133. sW = destWidth;
  134. sH = (destWidth * sHeight) / sWidth;
  135. }
  136. else
  137. {
  138. sH = destHeight;
  139. sW = (sWidth * destHeight) / sHeight;
  140. }
  141. }
  142. else
  143. {
  144. sW = sWidth;
  145. sH = sHeight;
  146. }
  147. Bitmap outBmp = new Bitmap(destWidth, destHeight);
  148. Graphics g = Graphics.FromImage(outBmp);
  149. g.Clear(Color.Transparent);
  150. // 设置画布的描绘质量
  151. g.CompositingQuality = CompositingQuality.HighQuality;
  152. g.SmoothingMode = SmoothingMode.HighQuality;
  153. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  154. g.DrawImage(imgSource, new Rectangle((destWidth - sW) / , (destHeight - sH) / , sW, sH), , , imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
  155. g.Dispose();
  156. // 以下代码为保存图片时,设置压缩质量
  157. EncoderParameters encoderParams = new EncoderParameters();
  158. long[] quality = new long[];
  159. quality[] = ;
  160. EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
  161. encoderParams.Param[] = encoderParam;
  162. imgSource.Dispose();
  163. return outBmp;
  164. }
  165. #endregion
  166. }

ImageUtility辅助类的更多相关文章

  1. Java的几个同步辅助类

    Java为我们提供了一些同步辅助类,利用这些辅助类我们可以在多线程编程中,灵活地把握线程的状态. CountDownLatch CountDownLatch一个同步辅助类,在完成一组正在其他线程中执行 ...

  2. ASP.NET Core 中文文档 第四章 MVC(3.6.2 )自定义标签辅助类(Tag Helpers)

    原文:Authoring Tag Helpers 作者:Rick Anderson 翻译:张海龙(jiechen) 校对:许登洋(Seay) 示例代码查看与下载 从 Tag Helper 讲起 本篇教 ...

  3. DateHelper.cs日期时间操作辅助类C#

    //==================================================================== //** Copyright © classbao.com ...

  4. 同步辅助类CountDownLatch用法

    CountDownLatch是一个同步辅助类,犹如倒计时计数器,创建对象时通过构造方法设置初始值,调用CountDownLatch对象的await()方法则使当前线程处于等待状态,调用countDow ...

  5. 基于MemoryCache的缓存辅助类

    背景: 1. 什么是MemoryCache? memoryCache就是用电脑内存做缓存处理 2.使用范围? 可用于不常变的数据,进行保存在内存中,提高处理效率 代码: /// <summary ...

  6. java中被各种XXUtil/XXUtils辅助类恶心到了,推荐这种命名方法

    且看一下有多少个StringUtils 列举一下XXUtil/XXUtils恶劣之处 1. 不知道该用XXUtil还是用XXUtils, 或者XXHelper, XXTool 2. 不知道该用a.ja ...

  7. NPOI操作Excel辅助类

    /// <summary> /// NPOI操作excel辅助类 /// </summary> public static class NPOIHelper { #region ...

  8. ByteBuf和相关辅助类

    当我们进行数据传输的时候,往往需要使用到缓冲区,常用的缓冲区就是JDK NIO类库提供的java.nio.Buffer. 实际上,7种基础类型(Boolean除外)都有自己的缓冲区实现,对于NIO编程 ...

  9. Bootstrap<基础九>辅助类

    Bootstrap 中的一些可能会派上用场的辅助类. 文本 以下不同的类展示了不同的文本颜色.如果文本是个链接鼠标移动到文本上会变暗: 类 描述   .text-muted "text-mu ...

随机推荐

  1. LSB MSB

    #LSB:(Least Significant,Bit) 最低有效位 :MSB(Most Significant Bit):最高有效位,若MSB=1,则表示数据为负值,若MSB=0则表示数据为正. 在 ...

  2. Centos7服务器搭建网络家园和论坛

    环境:Centos7 工具:mysql,php,httpd 目的:熟练掌握httpd服务器搭建和个服务器之间的配合. 有兴趣的朋友可以来实践一下,我会提供各种源码进行搭建. 网络家园和论坛源码:htt ...

  3. CnetOS6.7编译安装MariaDB

    --安装所需软件包 [root@localhost mariadb-10.1.14]# yum install bison bison-devel ncurses libxml2 libxml2-de ...

  4. 201871010105-曹玉中《面向对象程序设计(java)》第十一周学习总结

    201871010105-曹玉中<面向对象程序设计(java)>第十一周学习总结 项目 内容 <面向对象程序设计(java)> https://www.cnblogs.com/ ...

  5. Linux简单命令的使用

    1.linux上怎么快速删除一个目录在linux中删除一个目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可解决.直接rm就可以了,不过要加两 ...

  6. 重新学习SpringMVC——基础

    2. SpringMVC_HelloWorld3. SpringMVC_RequestMapping_修饰类4. SpringMVC_RequestMapping_请求方式5. SpringMVC_R ...

  7. Junit框架使用(4)--JUnit常用断言及注解

    从别人博客中抄过来一点东西 原文地址:http://blog.csdn.net/wangpeng047/article/details/9628449 断言是编写测试用例的核心实现方式,即期望值是多少 ...

  8. LG2766 最长不下降子序列问题 最大流 网络流24题

    问题描述 LG2766 题解 \(\mathrm{Subtask 1}\) 一个求最长不下降子序列的问题,发现\(n \le 500\),直接\(O(n^2)\)暴力DP即可. \(\mathrm{S ...

  9. [LeetCode] 763. Partition Labels 分割标签

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  10. [LeetCode] 380. Insert Delete GetRandom O(1) 常数时间内插入删除和获得随机数

    Design a data structure that supports all following operations in average O(1) time. insert(val): In ...