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辅助类的更多相关文章

  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. c++和c动态申请二维数组

    这是我面试中遇到的一道题,用c和c++分别申请一个二维数组,int **res,要求申请后的可以使用res[3][4]这一类防存方式. 这个是没有错误检查的版本. 答案: c++语言的版本 int * ...

  2. 解决Vue调用springboot接口403跨域问题

    最近在做一个前后端分离的项目, 前端用的是Vue后端使用的是springboot, 在项目整合的时候发现前端调用后端接口报错403跨域请求问题 前端跨域请求已解决, 那么问题就出在后端了, 找了一些资 ...

  3. mysql常用配置注意项与sql优化

    建立数据库: 建立数据库时编码字符集采用utf8 排序规则: 后缀"_cs"或者"_ci"意思是区分大小写和不区分大小写(Case Sensitive & ...

  4. 201871010121-王方-《面向对象程序设计(java)》第十二周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p/ ...

  5. USACO wormhole

    洛谷 P1444 [USACO1.3]虫洞wormhole https://www.luogu.org/problemnew/show/P1444 JDOJ 2386: USACO 2013 Dec ...

  6. 个人网站 & 博客

    www.ykmimi.com (测试使用) http://www.implements.fun/  (测试使用) http://www.java-developer.cn/  (测试使用,预备论坛) ...

  7. AcWing 91. 最短Hamilton路径

    今天第一次在\(AcWing\)这个网站上做题,来发一下此网站的第一篇题解 传送门 思路 直接枚举的话时间复杂度为\(O(n*n!)\) 复杂度显然爆炸,所以我们用二进制枚举,这样就可以把复杂度降到\ ...

  8. 洛谷 P5021 [NOIP2018]赛道重建

    洛谷 P5021 [NOIP2018]赛道重建 传送门 思路 思路就是常规的思路,所以就不说了--我就是来记录一下我的\(AC\)之路的,真的是太爽了 没错--我也是一个个打的部分分,最后终于AC的, ...

  9. [ Python入门教程 ] Python基础语法

    Python的语法非常简练,因此用Python编写的程序可读性强.容易理解.本章将介绍Python的基本语法和概念. Python文件类型 1.源代码.Python的源代码的扩展名以py结尾,可直接运 ...

  10. [LeetCode] 785. Is Graph Bipartite? 是二分图么?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...