您的位置:首页 > 其它

.Net 实现图片缩略图上传通用方法

2018-08-02 09:30 656 查看

日常开发中,经常碰到图片上传的需求,尤其在商城系统开发的时候,商品列表商品图片展示如果使用高清原图,由于高清原图比较大,加载原图时间会大大增加,直接导致系统性能底下,用户体验不好,并发量高的时候直接就挂掉了,这时候后台上传图片的时候,就必须将原高清图进行压缩,生成高质量缩略图,然后在商品列表读取缩略图可以大大减少加载时间,起到一个性能优化的作用,当然在商品详情的时候还是得用高清原图!

以下代码,可以在实际开发中使用将图片高质量压缩,话不多说,代码贴下:

/// <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 = 0, int height = 0, string mode = "", int flag = 100, int size = 0)
{
Image sourceImage = null;
Image bitmap = null;
Graphics g = null;
EncoderParameters ep = null;
EncoderParameter eParam = null;
try
{
sourceImage = Image.FromFile(sourcePath);
int toWidth = 0;
if (width > 0)
{
toWidth = width;
}
else
{
toWidth = sourceImage.Width;
}
int toHeight = 0;
if (height > 0)
{
toHeight = height;
}
else
{
toHeight = sourceImage.Height;
}
int x = 0;
int y = 0;
int ow = sourceImage.Width;
int oh = sourceImage.Height;
if (width > 0 && height > 0 && !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 = 0;
x = (sourceImage.Width - ow) / 2;
}
else
{
ow = sourceImage.Width;
oh = sourceImage.Width * height / toWidth;
x = 0;
y = (sourceImage.Height - oh) / 2;
}
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(0, 0, toWidth, toHeight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
//以下代码为保存图片时,设置压缩质量
ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag;//设置压缩的比例1-100
eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();//获取图像编码器的信息
ImageCodecInfo jpegICIinfo = null;
for (int i = 0; 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 > 0 && fiTarget.Length > 1024 * size)
{
flag = flag - 10;
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();
}
}
}

总结

以上所述是小编给大家介绍的.Net 实现图片缩略图上传通用方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  .net 缩络图 上传