ImageUtility辅助类
- 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 = ;
- float dpix = qrImg.HorizontalResolution;
- float dpiy = qrImg.VerticalResolution;
- var _newWidth = ( * qrImg.Width - * margin) * 1.0f / ;
- 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 = InterpolationMode.HighQualityBicubic;
- g.SmoothingMode = SmoothingMode.HighQuality;
- g.Clear(Color.Transparent);
- Pen p = new Pen(new SolidBrush(Color.White));
- Rectangle rect = new Rectangle(, , newImgWidth - , newImgWidth - );
- using (GraphicsPath path = CreateRoundedRectanglePath(rect, ))
- {
- 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 = InterpolationMode.HighQualityBicubic;
- g1.SmoothingMode = SmoothingMode.HighQuality;
- g1.Clear(Color.Transparent);
- Pen p1 = new Pen(new SolidBrush(Color.Gray));
- Rectangle rect1 = new Rectangle(, , _headerImg.Width - , _headerImg.Width - );
- using (GraphicsPath path1 = CreateRoundedRectanglePath(rect1, ))
- {
- g1.DrawPath(p1, path1);
- TextureBrush brush = new TextureBrush(_headerImg);
- g1.FillPath(brush, path1);
- }
- g1.Dispose();
- PointF center = new PointF((newImgWidth - _headerImg.Width) / , (newImgWidth - _headerImg.Height) / );
- 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, , );
- PointF center2 = new PointF((qrImg.Width - headerBgImg.Width) / , (qrImg.Height - headerBgImg.Height) / );
- 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 * , cornerRadius * , , );
- roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * , rect.Y);
- roundedRect.AddArc(rect.X + rect.Width - cornerRadius * , rect.Y, cornerRadius * , cornerRadius * , , );
- roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * , rect.Right, rect.Y + rect.Height - cornerRadius * );
- roundedRect.AddArc(rect.X + rect.Width - cornerRadius * , rect.Y + rect.Height - cornerRadius * , cornerRadius * , cornerRadius * , , );
- roundedRect.AddLine(rect.Right - cornerRadius * , rect.Bottom, rect.X + cornerRadius * , rect.Bottom);
- roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * , cornerRadius * , cornerRadius * , , );
- roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * , rect.X, rect.Y + cornerRadius * );
- 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图片
- Image newImage = new Bitmap((int)newWidth, (int)newHeight);
- //新建一个画板
- Graphics newG = Graphics.FromImage(newImage);
- //设置质量
- newG.InterpolationMode = InterpolationMode.HighQualityBicubic;
- newG.SmoothingMode = SmoothingMode.HighQuality;
- //置背景色
- newG.Clear(Color.Transparent);
- //画图
- newG.DrawImage(initImage, new Rectangle(, , newImage.Width, newImage.Height), new Rectangle(, , initImage.Width, initImage.Height), GraphicsUnit.Pixel);
- newG.Dispose();
- return newImage;
- }
- /// <summary>
- /// 创建缩略图
- /// </summary>
- /// <param name="b"></param>
- /// <param name="destHeight"></param>
- /// <param name="destWidth"></param>
- /// <returns></returns>
- public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidth)
- {
- Image imgSource = b;
- ImageFormat thisFormat = imgSource.RawFormat;
- int sW = , sH = ;
- // 按比例缩放
- 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) / , (destHeight - sH) / , sW, sH), , , imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
- g.Dispose();
- // 以下代码为保存图片时,设置压缩质量
- EncoderParameters encoderParams = new EncoderParameters();
- long[] quality = new long[];
- quality[] = ;
- EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
- encoderParams.Param[] = encoderParam;
- imgSource.Dispose();
- return outBmp;
- }
- #endregion
- }
ImageUtility辅助类的更多相关文章
- Java的几个同步辅助类
Java为我们提供了一些同步辅助类,利用这些辅助类我们可以在多线程编程中,灵活地把握线程的状态. CountDownLatch CountDownLatch一个同步辅助类,在完成一组正在其他线程中执行 ...
- ASP.NET Core 中文文档 第四章 MVC(3.6.2 )自定义标签辅助类(Tag Helpers)
原文:Authoring Tag Helpers 作者:Rick Anderson 翻译:张海龙(jiechen) 校对:许登洋(Seay) 示例代码查看与下载 从 Tag Helper 讲起 本篇教 ...
- DateHelper.cs日期时间操作辅助类C#
//==================================================================== //** Copyright © classbao.com ...
- 同步辅助类CountDownLatch用法
CountDownLatch是一个同步辅助类,犹如倒计时计数器,创建对象时通过构造方法设置初始值,调用CountDownLatch对象的await()方法则使当前线程处于等待状态,调用countDow ...
- 基于MemoryCache的缓存辅助类
背景: 1. 什么是MemoryCache? memoryCache就是用电脑内存做缓存处理 2.使用范围? 可用于不常变的数据,进行保存在内存中,提高处理效率 代码: /// <summary ...
- java中被各种XXUtil/XXUtils辅助类恶心到了,推荐这种命名方法
且看一下有多少个StringUtils 列举一下XXUtil/XXUtils恶劣之处 1. 不知道该用XXUtil还是用XXUtils, 或者XXHelper, XXTool 2. 不知道该用a.ja ...
- NPOI操作Excel辅助类
/// <summary> /// NPOI操作excel辅助类 /// </summary> public static class NPOIHelper { #region ...
- ByteBuf和相关辅助类
当我们进行数据传输的时候,往往需要使用到缓冲区,常用的缓冲区就是JDK NIO类库提供的java.nio.Buffer. 实际上,7种基础类型(Boolean除外)都有自己的缓冲区实现,对于NIO编程 ...
- Bootstrap<基础九>辅助类
Bootstrap 中的一些可能会派上用场的辅助类. 文本 以下不同的类展示了不同的文本颜色.如果文本是个链接鼠标移动到文本上会变暗: 类 描述 .text-muted "text-mu ...
随机推荐
- LSB MSB
#LSB:(Least Significant,Bit) 最低有效位 :MSB(Most Significant Bit):最高有效位,若MSB=1,则表示数据为负值,若MSB=0则表示数据为正. 在 ...
- Centos7服务器搭建网络家园和论坛
环境:Centos7 工具:mysql,php,httpd 目的:熟练掌握httpd服务器搭建和个服务器之间的配合. 有兴趣的朋友可以来实践一下,我会提供各种源码进行搭建. 网络家园和论坛源码:htt ...
- CnetOS6.7编译安装MariaDB
--安装所需软件包 [root@localhost mariadb-10.1.14]# yum install bison bison-devel ncurses libxml2 libxml2-de ...
- 201871010105-曹玉中《面向对象程序设计(java)》第十一周学习总结
201871010105-曹玉中<面向对象程序设计(java)>第十一周学习总结 项目 内容 <面向对象程序设计(java)> https://www.cnblogs.com/ ...
- Linux简单命令的使用
1.linux上怎么快速删除一个目录在linux中删除一个目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可解决.直接rm就可以了,不过要加两 ...
- 重新学习SpringMVC——基础
2. SpringMVC_HelloWorld3. SpringMVC_RequestMapping_修饰类4. SpringMVC_RequestMapping_请求方式5. SpringMVC_R ...
- Junit框架使用(4)--JUnit常用断言及注解
从别人博客中抄过来一点东西 原文地址:http://blog.csdn.net/wangpeng047/article/details/9628449 断言是编写测试用例的核心实现方式,即期望值是多少 ...
- LG2766 最长不下降子序列问题 最大流 网络流24题
问题描述 LG2766 题解 \(\mathrm{Subtask 1}\) 一个求最长不下降子序列的问题,发现\(n \le 500\),直接\(O(n^2)\)暴力DP即可. \(\mathrm{S ...
- [LeetCode] 763. Partition Labels 分割标签
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- [LeetCode] 380. Insert Delete GetRandom O(1) 常数时间内插入删除和获得随机数
Design a data structure that supports all following operations in average O(1) time. insert(val): In ...