/// <summary>
/// 图片处理工具类
/// </summary>
public class ImageUtils
{
/// <summary>
/// 产生缩略图
/// </summary>
public static System.IO.Stream GenerateThumbnail(System.IO.Stream imageStream, int width, int height)
{
System.IO.Stream ms;
using (var originalImage = System.Drawing.Image.FromStream(imageStream))
{
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight))
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);
ms = new System.IO.MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
return ms;
}
/// <summary>
/// 产生缩略图
/// </summary>
public static void GenerateThumbnail(System.IO.Stream imageStream, int width, int height, string outputFilePath)
{
using (var stream = GenerateThumbnail(imageStream, width, height))
using (var fileStream = System.IO.File.Open(outputFilePath, System.IO.FileMode.OpenOrCreate))
{
((System.IO.MemoryStream)stream).WriteTo(fileStream);
}
}
/// <summary>
/// 产生缩略图
/// </summary>
public static System.IO.Stream GenerateThumbnail(byte[] imageBytes, int width, int height)
{
System.IO.Stream ms;
using (var stream = new System.IO.MemoryStream(imageBytes))
{
ms = GenerateThumbnail(stream, width, height);
}
return ms;
}
/// <summary>
/// 产生缩略图
/// </summary>
public static void GenerateThumbnail(byte[] imageBytes, int width, int height, string outputFilePath)
{
using (var stream = GenerateThumbnail(imageBytes, width, height))
using (var fileStream = System.IO.File.Open(outputFilePath, System.IO.FileMode.OpenOrCreate))
{
((System.IO.MemoryStream)stream).WriteTo(fileStream);
}
}
/// <summary>
/// 产生缩略图
/// </summary>
public static System.IO.Stream GenerateThumbnail(string imageFilePath, int width, int height)
{
System.IO.Stream ms;
using (var stream = new System.IO.FileStream(imageFilePath, System.IO.FileMode.Open))
{
ms = GenerateThumbnail(stream, width, height);
}
return ms;
}
/// <summary>
/// 产生缩略图
/// </summary>
public static void GenerateThumbnail(string imageFilePath, int width, int height, string outputFilePath)
{
using (var stream = GenerateThumbnail(imageFilePath, width, height))
using (var fileStream = System.IO.File.Open(outputFilePath, System.IO.FileMode.OpenOrCreate))
{
((System.IO.MemoryStream)stream).WriteTo(fileStream);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述