C#.NET 缩略图处理类CutImage
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace Pub.Class {
/// <summary>
/// 缩图处理类
/// </summary>
public class CutImage
{
#region 构造函数
public CutImage() { }
#endregion
#region 私有成员
private int width;
private int height;
private int size;
private string errMSG;
#endregion
#region 公共属性
/// <summary>
/// 图片宽度
/// </summary>
public int Width { get { return width; } set { width = value; } }
/// <summary>
/// 图片高度
/// </summary>
public int Height { get { return height; } set { Height = value; } }
/// <summary>
/// 图片尺寸
/// </summary>
public int Size { get { return size; } set { size = value; } }
/// <summary>
/// 错误消息
/// </summary>
public string ErrMSG { get { return this.errMSG; } set { this.errMSG = value; } }
#endregion
#region 公共方法
/// <summary>
/// 获取图片信息
/// </summary>
/// <param name="imagePath">图片地址</param>
/// <returns>成功true失败false</returns>
public bool GetImage(string imagePath) {
try {
System.Drawing.Image image = System.Drawing.Image.FromFile(imagePath);
this.width = image.Width;
this.height = image.Height;
image.Dispose();
FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
this.size = (int)fs.Length;
fs.Close();
return true;
} catch (Exception ex) {
this.errMSG = ex.ToString();
return false;
}
}
/// <summary>
/// 按宽X长比例切图
/// </summary>
/// <param name="imagePath">源图地址</param>
/// <param name="savePath">新图地址</param>
/// <param name="cutWidth">宽度</param>
/// <param name="cutHeight">高度</param>
/// <returns>成功true失败false</returns>
public bool CutImageCustomMin(string imagePath, string savePath, int cutWidth, int cutHeight) {
try {
System.Drawing.Image objImage = System.Drawing.Image.FromFile(imagePath);
float x = objImage.Width;
float y = objImage.Height;
float xPercent = x / cutWidth;
float yPercent = y / cutHeight;
if (xPercent < yPercent) {
this.width = (int)((x * cutHeight) / y);
this.height = cutHeight;
} else {
this.width = cutWidth;
this.height = (int)((cutWidth * y) / x);
}
System.Drawing.Image newimage = new Bitmap(objImage.Width, objImage.Height, PixelFormat.Format32bppRgb);
Graphics g = Graphics.FromImage(newimage);
g.DrawImage(objImage, 0, 0, objImage.Width, objImage.Height);
g.Dispose();
System.Drawing.Image thumbImage = newimage.GetThumbnailImage(this.width, this.height, null, IntPtr.Zero);
thumbImage.Save(savePath, objImage.RawFormat);
objImage.Dispose();
newimage.Dispose();
thumbImage.Dispose();
FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.Read);
this.size = (int)fs.Length;
fs.Close();
return true;
} catch (Exception ex) {
this.errMSG = ex.ToString();
return false;
}
}
/// <summary>
/// 按宽X长比例切图
/// </summary>
/// <param name="imagePath">源图地址</param>
/// <param name="savePath">新图地址</param>
/// <param name="cutWidth">宽度</param>
/// <param name="cutHeight">高度</param>
/// <returns>成功true失败false</returns>
public bool CutImageCustom(string imagePath, string savePath, int cutWidth, int cutHeight) {
try {
System.Drawing.Image objImage = System.Drawing.Image.FromFile(imagePath);
float x = objImage.Width;
float y = objImage.Height;
float xPercent = x / cutWidth;
float yPercent = y / cutHeight;
if (xPercent < yPercent) {
this.width = (int)((x * cutHeight) / y);
this.height = cutHeight;
} else {
this.width = cutWidth;
this.height = (int)((cutWidth * y) / x);
}
Bitmap newimage = new Bitmap(this.width, this.height, PixelFormat.Format32bppRgb);
newimage.SetResolution(72f, 72f);
Graphics gdiobj = Graphics.FromImage(newimage);
gdiobj.CompositingQuality = CompositingQuality.HighQuality;
gdiobj.SmoothingMode = SmoothingMode.HighQuality;
gdiobj.InterpolationMode = InterpolationMode.HighQualityBicubic;
gdiobj.PixelOffsetMode = PixelOffsetMode.HighQuality;
gdiobj.FillRectangle(new SolidBrush(Color.White), 0, 0, this.width, this.height);
Rectangle destrect = new Rectangle(0, 0, this.width, this.height);
gdiobj.DrawImage(objImage, destrect, 0, 0, objImage.Width, objImage.Height, GraphicsUnit.Pixel);
gdiobj.Dispose();
System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs) {
if (codec.MimeType == "image/jpeg") { ici = codec; }
}
if (ici != null) newimage.Save(savePath, ici, ep); else newimage.Save(savePath, objImage.RawFormat);
objImage.Dispose();
newimage.Dispose();
FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.Read);
this.size = (int)fs.Length;
fs.Close();
return true;
} catch (Exception ex) {
this.errMSG = ex.ToString();
return false;
}
}
/// <summary>
/// 将图片缩放到指定的宽度
/// </summary>
/// <param name="imagePath">源图地址</param>
/// <param name="savePath">新图地址</param>
/// <param name="square">宽度</param>
/// <returns>成功true失败false</returns>
public bool CutImageByWidth(string imagePath, string savePath, int square) {
try {
int cutWidth = square;
System.Drawing.Image objImage = System.Drawing.Image.FromFile(imagePath);
float x = objImage.Width;
float y = objImage.Height;
this.width = cutWidth;
this.height = (int)((cutWidth * y) / x);
System.Drawing.Image newimage = new Bitmap(objImage.Width, objImage.Height, PixelFormat.Format32bppRgb);
Graphics g = Graphics.FromImage(newimage);
g.DrawImage(objImage, 0, 0, objImage.Width, objImage.Height);
g.Dispose();
System.Drawing.Image thumbImage = newimage.GetThumbnailImage(this.width, this.height, null, IntPtr.Zero);
thumbImage.Save(savePath, objImage.RawFormat);
objImage.Dispose();
newimage.Dispose();
thumbImage.Dispose();
FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.Read);
this.size = (int)fs.Length;
fs.Close();
return true;
} catch (Exception ex) {
this.errMSG = ex.ToString();
return false;
}
}
/// <summary>
/// 将图片缩放到指定的高度
/// </summary>
/// <param name="imagePath">源图地址</param>
/// <param name="savePath">新图地址</param>
/// <param name="square">高度</param>
/// <returns>成功true失败false</returns>
public bool CutImageByHeight(string imagePath, string savePath, int square) {
try {
int cutHeight = square;
System.Drawing.Image objImage = System.Drawing.Image.FromFile(imagePath);
float x = objImage.Width;
float y = objImage.Height;
this.height = cutHeight;
this.width = (int)((cutHeight * x) / y);
System.Drawing.Image newimage = new Bitmap(objImage.Width, objImage.Height, PixelFormat.Format32bppRgb);
Graphics g = Graphics.FromImage(newimage);
g.DrawImage(objImage, 0, 0, objImage.Width, objImage.Height);
g.Dispose();
System.Drawing.Image thumbImage = newimage.GetThumbnailImage(this.width, this.height, null, IntPtr.Zero);
thumbImage.Save(savePath, objImage.RawFormat);
objImage.Dispose();
newimage.Dispose();
thumbImage.Dispose();
FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.Read);
this.size = (int)fs.Length;
fs.Close();
return true;
} catch (Exception ex) {
this.errMSG = ex.ToString();
return false;
}
}
/// <summary>
/// 将图片剪切到一个正方形
/// </summary>
/// <param name="imagePath">源图地址</param>
/// <param name="savePath">新图地址</param>
/// <param name="square">正方形边长</param>
/// <returns>成功true失败false</returns>
public bool CutImageSquare(string imagePath, string savePath, int square) {
try {
this.width = square;
this.height = square;
int cutWidth = square;
int cutHeight = square;
System.Drawing.Image objImage = System.Drawing.Image.FromFile(imagePath);
if (objImage.Width >= objImage.Height) {
cutWidth = objImage.Height;
cutHeight = objImage.Height;
} else {
cutWidth = objImage.Width;
cutHeight = objImage.Width;
}
System.Drawing.Image newimage = new Bitmap(cutWidth, cutHeight, PixelFormat.Format32bppRgb);
Graphics g = Graphics.FromImage(newimage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Rectangle destRect = new Rectangle(0, 0, cutWidth, cutHeight);
Rectangle srcRect = new Rectangle(0, 0, cutWidth, cutHeight);
GraphicsUnit units = GraphicsUnit.Pixel;
g.DrawImage(objImage, destRect, srcRect, units);
g.Dispose();
System.Drawing.Image thumbImage = newimage.GetThumbnailImage(this.width, this.height, null, IntPtr.Zero);
thumbImage.Save(savePath, objImage.RawFormat);
objImage.Dispose();
newimage.Dispose();
thumbImage.Dispose();
FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.Read);
this.size = (int)fs.Length;
fs.Close();
return true;
} catch (Exception ex) {
this.errMSG = ex.ToString();
return false;
}
}
#region 处理图片长宽显示
public static void GetProperSize(int trueWidth, int trueHeight, int placeWidth, int placeHeight, out int showWidth, out int showHeight) {
if (trueHeight < placeHeight && trueWidth < placeWidth) {
showHeight = trueHeight;
showWidth = trueWidth;
} else {
float x = (float)trueWidth;
float y = (float)trueHeight;
float xPercent = x / placeWidth;
float yPercent = y / placeHeight;
if (xPercent < yPercent) {
showWidth = (int)((x * placeHeight) / y);
showHeight = placeHeight;
} else {
showWidth = placeWidth;
showHeight = (int)((placeWidth * y) / x);
}
}
}
#endregion
#endregion
}
}
开源:
https://github.com/hcxiong 欢迎收藏:)
【推荐】国内首个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的设计模式综述