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

转自:http://www.cnblogs.com/coding1016/p/4924181.html

【转】C# 生成二维码并且在中间加Logo(图片合并)的更多相关文章

  1. C# 生成二维码并且在中间加Logo

    今天做项目的时候有个在生成二维码并且在中间加入Logo的需求,动手试了几把,总感觉效果没有之前写的好,就翻出旧代码,果然还是熟悉的味道,生成一张效果图如下 左边是微信里面的,右边是我自己生成的 原理比 ...

  2. 生成二维码、条形码、带logo的二维码

    Nuget安装ZXing.Net,帮助类: using System; using System.Collections.Generic; using System.Drawing; using Sy ...

  3. Android_demo之生成二维码

    今天我们来学习一个自动生成二维码 的写法.我们经常能见到各种二维码,比如公众号的二维码,网址的,加好友的,支付的二维码等等.其实每一个二维码只是利用图片的形式展示出来的,实际是一些字符串.而这个字符串 ...

  4. iOS开发之生成二维码

    一.二维码的生成 从iOS7开始集成了二维码的生成和读取功能 此前被广泛使用的zbarsdk目前不支持64位处理器   1.二维码的内容(传统的条形码只能放数字) 纯文本 名片 URL   2.生成二 ...

  5. java--实现将文字生成二维码图片,并在中间附上logo,下方附上文字

    前段时间因为工作需要,要实现将一段文字或者url生成二维码,然后中间附上logo,下方正中间附上文字的功能. 上网找了几篇教程学习了下,由于没有保存借鉴的博文链接,所以就没po上参考文章的链接啦,感谢 ...

  6. java生成二维码(带logo)

    之前写过一篇不带logo的二维码实现方式,採用QRCode和ZXing两种方式 http://blog.csdn.net/xiaokui_wingfly/article/details/3947618 ...

  7. 使用PHP生成二维码图像

    1.PHP生成二维码图像的类QRcode http://www.phper.org.cn/?post=128 QRcode是用于生成二维条形码的开放源码 (LGPL) 库.提供 API 创建条码图像. ...

  8. Javascript生成二维码(QR)

    网络上已经有非常多的二维码编码和解码工具和代码,很多都是服务器端的,也就是说需要一台服务器才能提供二维码的生成.本着对服务器性能的考虑,这种小事情都让服务器去做,感觉对不住服务器,尤其是对于大流量的网 ...

  9. 使用jquery.qrcode生成二维码(转)

    jQuery 的 qrcode 插件就可以在浏览器端生成二维码图片. 这个插件的使用非常简单: 1.首先在页面中加入jquery库文件和qrcode插件. <script type=" ...

随机推荐

  1. IDEA使用技巧:CamelCasePlugin插件

    CamelCasePlugin是一款可以快速进行格式转换的工具,较常用到的是大小写转换.驼峰式转换等. 1.打开idea,然后打开设置.点击Plugins 2.快捷键shift+alt+u

  2. Android 自动化测试介绍

    1 介绍: 风格: 3, 4,

  3. 【LTE基础知识】SGLTE, SVLTE, CSFB, VoLTE【转】

    本文转载自:https://blog.csdn.net/henryghx/article/details/18416405 4G网络下实现语音通话功能的技术共有三种——VoLTE.SGLTE(GSM ...

  4. 用GDB调试Segmentation 段错误【转】

    本文转载自:http://blog.csdn.net/learnhard/article/details/4879834 调试Linux程序的时候,出现Segmentation Fault是最郁闷的事 ...

  5. sublimeText3最新教程-自带插件汉化(sublime-text_build-3175_amd64)

    一.可用注册码 1.更改dns 在linux下的目录是     /etc/hosts 在win7中,hosts文件的位置:C:\Windows\System32\drivers\etc 127.0.0 ...

  6. Elasticsearch 基础概念知识

    接近实时(NRT) Elasticsearch是一个接近实时的搜索平台.这意味着,从索引一个文档直到这个文档能够被搜索到有一个轻微的延迟(通常是1秒). 集群(cluster) 一个集群就是由一个或多 ...

  7. LOJ#2632. 「BalticOI 2011 Day1」打开灯泡 Switch the Lamp On

    题目描述 译自 BalticOI 2011 Day1 T3「Switch the Lamp On」有一种正方形的电路元件,在它的两组相对顶点中,有一组会用导线连接起来,另一组则不会.有 N×M 个这样 ...

  8. spring boot 多数据源 + 事务控制

    1,首先在启动类加上@EnableTransactionManagement注解 package cn.bforce.common; import org.springframework.boot.S ...

  9. The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 解决方法

    项目忽然出现 The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Pat ...

  10. 项目中的一个分页功能pagination

    项目中的一个分页功能pagination <script> //总页数 ; ; //分页总数量 $(function () { // $("#pagination"). ...