.Net 图片缩略图上传通用方法
2024-09-21 10:51:53
日常开发中,经常碰到图片上传的需求,尤其在商城系统开发的时候,商品列表商品图片展示如果使用高清原图,由于高清原图比较大,加载原图时间会大大增加,直接导致系统性能底下,用户体验不好,并发量高的时候直接就挂掉了,这时候后台上传图片的时候,就必须将原高清图进行压缩,生成高质量缩略图,然后在商品列表读取缩略图可以大大减少加载时间,起到一个性能优化的作用,当然在商品详情的时候还是得用高清原图!
以下代码,可以在实际开发中使用将图片高质量压缩,话不多说,代码贴下:
/// <summary>
/// 生成缩略图或质量压缩
/// </summary>
/// <param name="sourcePath">源图路径(物理路径)</param>
/// <param name="targetPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度,如果宽度为0则不缩略</param>
/// <param name="height">缩略图高度,如果高度为0则不缩略</param>
/// <param name="mode">生成缩略图的方式,默认为空,为空则不缩略高宽[HW 指定高宽缩放(不变形);W 指定宽,高按比例;H 指定高,宽按比例;CUT 指定高宽裁减(不变形)]</param>
/// <param name="flag">压缩质量(数字越小压缩率越高)1-100</param>
/// <param name="size">压缩后图片的最大大小,0为不限制大小</param>
public static void MakeThumbnail(string sourcePath, string targetPath, int width = , int height = , string mode = "", int flag = , int size = )
{
Image sourceImage = null;
Image bitmap = null;
Graphics g = null;
EncoderParameters ep = null;
EncoderParameter eParam = null;
try
{
sourceImage = Image.FromFile(sourcePath); int toWidth = ;
if (width > )
{
toWidth = width;
}
else
{
toWidth = sourceImage.Width;
} int toHeight = ;
if (height > )
{
toHeight = height;
}
else
{
toHeight = sourceImage.Height;
} int x = ;
int y = ;
int ow = sourceImage.Width;
int oh = sourceImage.Height; if (width > && height > && !string.IsNullOrWhiteSpace(mode))
{
switch (mode.ToUpper())
{
case "HW"://指定高宽缩放(不变形)
int tempheight = sourceImage.Height * width / sourceImage.Width;
if (tempheight > height)
{
toWidth = sourceImage.Width * height / sourceImage.Height;
}
else
{
toHeight = sourceImage.Height * width / sourceImage.Width;
}
break;
case "W"://指定宽,高按比例
toHeight = sourceImage.Height * width / sourceImage.Width;
break;
case "H"://指定高,宽按比例
toWidth = sourceImage.Width * height / sourceImage.Height;
break;
case "CUT"://指定高宽裁减(不变形)
if ((double)sourceImage.Width / (double)sourceImage.Height > (double)toWidth / (double)toHeight)
{
oh = sourceImage.Height;
ow = sourceImage.Height * toWidth / toHeight;
y = ;
x = (sourceImage.Width - ow) / ;
}
else
{
ow = sourceImage.Width;
oh = sourceImage.Width * height / toWidth;
x = ;
y = (sourceImage.Height - oh) / ;
}
break;
}
} //新建一个bmp图片
bitmap = new Bitmap(toWidth, toHeight); //新建一个画板
g = Graphics.FromImage(bitmap); g.CompositingQuality = CompositingQuality.HighQuality; //设置高质量插值法
g.InterpolationMode = InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(sourceImage, new Rectangle(, , toWidth, toHeight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel); //以下代码为保存图片时,设置压缩质量
ep = new EncoderParameters();
long[] qy = new long[];
qy[] = flag;//设置压缩的比例1-100
eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[] = eParam; ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();//获取图像编码器的信息
ImageCodecInfo jpegICIinfo = null;
for (int i = ; i < arrayICI.Length; i++)
{
if (arrayICI[i].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[i];
break;
}
} if (jpegICIinfo != null)
{
bitmap.Save(targetPath, jpegICIinfo, ep);
FileInfo fiTarget = new FileInfo(targetPath);
if (size > && fiTarget.Length > * size)
{
flag = flag - ;
MakeThumbnail(sourcePath, targetPath, width, height, mode, flag, size);
}
}
else
{
//以jpg格式保存缩略图
bitmap.Save(targetPath, ImageFormat.Jpeg);
} }
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (sourceImage != null)
{
sourceImage.Dispose();
}
if (bitmap != null)
{
bitmap.Dispose();
}
if (g != null)
{
g.Dispose();
}
if (ep != null)
{
ep.Dispose();
}
if (eParam != null)
{
eParam.Dispose();
}
}
}
最新文章
- jquery版的全选,全不选和反选
- shell脚本监控MySQL服务是否正常
- python threading编程中的LOCK和RLOCK(可重入锁)
- jQuery-事件以及动画
- Daily Scrum – 1/7
- 限制Input输入类型的常见代码集合
- 响应式设计Responsinator工具推荐
- Codeforces Round #326 (Div. 2) D. Duff in Beach dp
- Android Development Tools 发生checkAndLoadTargetData错误
- chrome主页被篡改 成hao123
- 201521123049 《JAVA程序设计》 第2周学习总结
- RecyclerViewItemTouchHelperDemo【使用ItemTouchHelper进行拖拽排序功能】
- SQLServer低版本附加高版本的数据库常用处理方法
- 【数据结构】KMP算法
- 深度学习(九) 深度学习最全优化方法总结比较(SGD,Momentum,Nesterov Momentum,Adagrad,Adadelta,RMSprop,Adam)
- .Net语言 APP开发平台——Smobiler学习日志:如何调用API进行短信发送
- 求最近点对算法分析 closest pair algorithm
- CentOS7 linux 中提示 bash: ls: 未找到命令...
- C语言中#ifdef,#ifndef和#endif的作用
- 一:elasticsearch常用操作总结