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. 《高性能 Go 代码工坊》中译

    深入研究 Go 应用性能提升的英语系列文章,这里是中译 https://www.yuque.com/ksco/uiondt

  2. static、const、extern等关键字

    static 参考:https://blog.csdn.net/guotianqing/article/details/79828100 http://c.biancheng.net/view/222 ...

  3. Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet

    org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n ...

  4. MySQL 数据库 查询语句的基本操作,单表查询,多表查询

    1.查询语句的基本操作 - select - from - where - group by - having - distinct - order by - limit - 聚合函数: count, ...

  5. LG2463/BZOJ4698 「SDOI2008」Sandy的卡片 后缀数组

    问题描述 LG2463 BZOJ4698 题解 看到\(n\)个数串,一开始不太好处理,可以很容易想到把这\(n\)个数串连到一起,形成一个大串,但是每个串之间不容易处理. 经过思考,想到在每个串中间 ...

  6. flink源码学习

    传送门: https://www.jianshu.com/c/c9b356caf8a6

  7. LOJ6686 Stupid GCD(数论,欧拉函数,杜教筛)

    做题重心转移到 LOJ 了. 至于为什么,如果你知道“……”的密码,就去看吧. LOJ 上用户自创题大多数都不可做,今天看到个可做题(而且还是个水题),就来做了一发. 明显枚举立方根.(以下令 $m= ...

  8. java大作业博客--购物车

    Java 大作业----使用MySQL的购物车 一.团队介绍 姓名 任务 李天明.康友煌 GUI设计及代码编写 谢晓淞 业务代码编写.MySQL服务器平台部署.git代码库 严威 类和包的结构关系设计 ...

  9. Log-Structured Merge Tree (LSM Tree)

    一种树,适合于写多读少的场景.主要是利用了延迟更新.批量写.顺序写磁盘(磁盘sequence access比random access快). 背景 回顾数据存储的两个“极端”发展方向 加快读:加索引( ...

  10. [LeetCode] 834. Sum of Distances in Tree 树中距离之和

    An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge co ...