【转】C# 生成二维码并且在中间加Logo(图片合并)
- public class QRCodeHelper
- {
- public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidth)
- {
- System.Drawing.Image imgSource = b;
- System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat;
- int sW = 0, sH = 0;
- // 按比例缩放
- int sWidth = imgSource.Width;
- int sHeight = imgSource.Height;
- if (sHeight > destHeight || sWidth > destWidth)
- {
- if ((sWidth * destHeight) > (sHeight * destWidth))
- {
- sW = destWidth;
- sH = (destWidth * sHeight) / sWidth;
- }
- else
- {
- sH = destHeight;
- sW = (sWidth * destHeight) / sHeight;
- }
- }
- else
- {
- sW = sWidth;
- sH = sHeight;
- }
- Bitmap outBmp = new Bitmap(destWidth, destHeight);
- Graphics g = Graphics.FromImage(outBmp);
- g.Clear(Color.Transparent);
- // 设置画布的描绘质量
- g.CompositingQuality = CompositingQuality.HighQuality;
- g.SmoothingMode = SmoothingMode.HighQuality;
- g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
- g.Dispose();
- // 以下代码为保存图片时,设置压缩质量
- EncoderParameters encoderParams = new EncoderParameters();
- long[] quality = new long[1];
- quality[0] = 100;
- EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
- encoderParams.Param[0] = encoderParam;
- imgSource.Dispose();
- return outBmp;
- }
- public static Bitmap Create(string content, int size)
- {
- try
- {
- var options = new QrCodeEncodingOptions
- {
- DisableECI = true,
- CharacterSet = "UTF-8",
- Width = size,
- Height = size,
- Margin = 0,
- ErrorCorrection = ErrorCorrectionLevel.H
- };
- var writer = new BarcodeWriter();
- writer.Format = BarcodeFormat.QR_CODE;
- writer.Options = options;
- var bmp = writer.Write(content);
- return bmp;
- }
- catch (Exception ex)
- {
- return null;
- }
- }
- }
- public class ImageUtility
- {
- #region 合并用户QR图片和用户头像
- /// <summary>
- /// 合并用户QR图片和用户头像
- /// </summary>
- /// <param name="qrImg">QR图片</param>
- /// <param name="headerImg">用户头像</param>
- /// <param name="n">缩放比例</param>
- /// <returns></returns>
- public Bitmap MergeQrImg(Bitmap qrImg, Bitmap headerImg, double n = 0.23)
- {
- int margin = 10;
- float dpix = qrImg.HorizontalResolution;
- float dpiy = qrImg.VerticalResolution;
- var _newWidth = (10 * qrImg.Width - 46 * margin) * 1.0f / 46;
- var _headerImg = ZoomPic(headerImg, _newWidth / headerImg.Width);
- //处理头像
- int newImgWidth = _headerImg.Width + margin;
- Bitmap headerBgImg = new Bitmap(newImgWidth, newImgWidth);
- headerBgImg.MakeTransparent();
- Graphics g = Graphics.FromImage(headerBgImg);
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- g.Clear(Color.Transparent);
- Pen p = new Pen(new SolidBrush(Color.White));
- Rectangle rect = new Rectangle(0, 0, newImgWidth - 1, newImgWidth - 1);
- using (GraphicsPath path = CreateRoundedRectanglePath(rect, 7))
- {
- g.DrawPath(p, path);
- g.FillPath(new SolidBrush(Color.White), path);
- }
- //画头像
- Bitmap img1 = new Bitmap(_headerImg.Width, _headerImg.Width);
- Graphics g1 = Graphics.FromImage(img1);
- g1.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
- g1.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- g1.Clear(Color.Transparent);
- Pen p1 = new Pen(new SolidBrush(Color.Gray));
- Rectangle rect1 = new Rectangle(0, 0, _headerImg.Width - 1, _headerImg.Width - 1);
- using (GraphicsPath path1 = CreateRoundedRectanglePath(rect1, 7))
- {
- g1.DrawPath(p1, path1);
- TextureBrush brush = new TextureBrush(_headerImg);
- g1.FillPath(brush, path1);
- }
- g1.Dispose();
- PointF center = new PointF((newImgWidth - _headerImg.Width) / 2, (newImgWidth - _headerImg.Height) / 2);
- g.DrawImage(img1, center.X, center.Y, _headerImg.Width, _headerImg.Height);
- g.Dispose();
- Bitmap backgroudImg = new Bitmap(qrImg.Width, qrImg.Height);
- backgroudImg.MakeTransparent();
- backgroudImg.SetResolution(dpix, dpiy);
- headerBgImg.SetResolution(dpix, dpiy);
- Graphics g2 = Graphics.FromImage(backgroudImg);
- g2.Clear(Color.Transparent);
- g2.DrawImage(qrImg, 0, 0);
- PointF center2 = new PointF((qrImg.Width - headerBgImg.Width) / 2, (qrImg.Height - headerBgImg.Height) / 2);
- g2.DrawImage(headerBgImg, center2);
- g2.Dispose();
- return backgroudImg;
- }
- #endregion
- #region 图形处理
- /// <summary>
- /// 创建圆角矩形
- /// </summary>
- /// <param name="rect">区域</param>
- /// <param name="cornerRadius">圆角角度</param>
- /// <returns></returns>
- private GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
- {
- //下午重新整理下,圆角矩形
- GraphicsPath roundedRect = new GraphicsPath();
- roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
- roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
- roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
- roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
- roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
- roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
- roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
- roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
- roundedRect.CloseFigure();
- return roundedRect;
- }
- /// <summary>
- /// 图片按比例缩放
- /// </summary>
- private Image ZoomPic(Image initImage, double n)
- {
- //缩略图宽、高计算
- double newWidth = initImage.Width;
- double newHeight = initImage.Height;
- newWidth = n * initImage.Width;
- newHeight = n * initImage.Height;
- //生成新图
- //新建一个bmp图片
- System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);
- //新建一个画板
- System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);
- //设置质量
- newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
- newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- //置背景色
- newG.Clear(Color.Transparent);
- //画图
- 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);
- newG.Dispose();
- return newImage;
- }
- #endregion
- }
转自:http://www.cnblogs.com/coding1016/p/4924181.html
【转】C# 生成二维码并且在中间加Logo(图片合并)的更多相关文章
- C# 生成二维码并且在中间加Logo
今天做项目的时候有个在生成二维码并且在中间加入Logo的需求,动手试了几把,总感觉效果没有之前写的好,就翻出旧代码,果然还是熟悉的味道,生成一张效果图如下 左边是微信里面的,右边是我自己生成的 原理比 ...
- 生成二维码、条形码、带logo的二维码
Nuget安装ZXing.Net,帮助类: using System; using System.Collections.Generic; using System.Drawing; using Sy ...
- Android_demo之生成二维码
今天我们来学习一个自动生成二维码 的写法.我们经常能见到各种二维码,比如公众号的二维码,网址的,加好友的,支付的二维码等等.其实每一个二维码只是利用图片的形式展示出来的,实际是一些字符串.而这个字符串 ...
- iOS开发之生成二维码
一.二维码的生成 从iOS7开始集成了二维码的生成和读取功能 此前被广泛使用的zbarsdk目前不支持64位处理器 1.二维码的内容(传统的条形码只能放数字) 纯文本 名片 URL 2.生成二 ...
- java--实现将文字生成二维码图片,并在中间附上logo,下方附上文字
前段时间因为工作需要,要实现将一段文字或者url生成二维码,然后中间附上logo,下方正中间附上文字的功能. 上网找了几篇教程学习了下,由于没有保存借鉴的博文链接,所以就没po上参考文章的链接啦,感谢 ...
- java生成二维码(带logo)
之前写过一篇不带logo的二维码实现方式,採用QRCode和ZXing两种方式 http://blog.csdn.net/xiaokui_wingfly/article/details/3947618 ...
- 使用PHP生成二维码图像
1.PHP生成二维码图像的类QRcode http://www.phper.org.cn/?post=128 QRcode是用于生成二维条形码的开放源码 (LGPL) 库.提供 API 创建条码图像. ...
- Javascript生成二维码(QR)
网络上已经有非常多的二维码编码和解码工具和代码,很多都是服务器端的,也就是说需要一台服务器才能提供二维码的生成.本着对服务器性能的考虑,这种小事情都让服务器去做,感觉对不住服务器,尤其是对于大流量的网 ...
- 使用jquery.qrcode生成二维码(转)
jQuery 的 qrcode 插件就可以在浏览器端生成二维码图片. 这个插件的使用非常简单: 1.首先在页面中加入jquery库文件和qrcode插件. <script type=" ...
随机推荐
- IDEA使用技巧:CamelCasePlugin插件
CamelCasePlugin是一款可以快速进行格式转换的工具,较常用到的是大小写转换.驼峰式转换等. 1.打开idea,然后打开设置.点击Plugins 2.快捷键shift+alt+u
- Android 自动化测试介绍
1 介绍: 风格: 3, 4,
- 【LTE基础知识】SGLTE, SVLTE, CSFB, VoLTE【转】
本文转载自:https://blog.csdn.net/henryghx/article/details/18416405 4G网络下实现语音通话功能的技术共有三种——VoLTE.SGLTE(GSM ...
- 用GDB调试Segmentation 段错误【转】
本文转载自:http://blog.csdn.net/learnhard/article/details/4879834 调试Linux程序的时候,出现Segmentation Fault是最郁闷的事 ...
- sublimeText3最新教程-自带插件汉化(sublime-text_build-3175_amd64)
一.可用注册码 1.更改dns 在linux下的目录是 /etc/hosts 在win7中,hosts文件的位置:C:\Windows\System32\drivers\etc 127.0.0 ...
- Elasticsearch 基础概念知识
接近实时(NRT) Elasticsearch是一个接近实时的搜索平台.这意味着,从索引一个文档直到这个文档能够被搜索到有一个轻微的延迟(通常是1秒). 集群(cluster) 一个集群就是由一个或多 ...
- LOJ#2632. 「BalticOI 2011 Day1」打开灯泡 Switch the Lamp On
题目描述 译自 BalticOI 2011 Day1 T3「Switch the Lamp On」有一种正方形的电路元件,在它的两组相对顶点中,有一组会用导线连接起来,另一组则不会.有 N×M 个这样 ...
- spring boot 多数据源 + 事务控制
1,首先在启动类加上@EnableTransactionManagement注解 package cn.bforce.common; import org.springframework.boot.S ...
- 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 ...
- 项目中的一个分页功能pagination
项目中的一个分页功能pagination <script> //总页数 ; ; //分页总数量 $(function () { // $("#pagination"). ...