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

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

         /// <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();
}
}
}

.Net 图片缩略图上传通用方法的更多相关文章

  1. ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结

    相册 iphone的相册包含摄像头胶卷+用户计算机同步的部分照片.用户可以通过UIImagePickerController类提供的交互对话框来从相册中选择图像.但是,注意:相册中的图片机器路径无法直 ...

  2. ios中摄像头/相册获取图片压缩图片上传服务器方法总结

    本文章介绍了关于ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结,有需要了解的同学可以参考一下下.     这几天在搞iphone上面一个应用的开发,里面有需要摄像头/相册编程和图片上传的问 ...

  3. Web文件(图片)上传方法

    在开放Web应用程序的时候经常会遇到图片或者是文件上传的模块,这里就是该模块的实现的后台方法 上传图片方法 /// <summary> /// 功能:上传图片方法 /// </sum ...

  4. 项目总结07:JS图片的上传预览和表单提交(FileReader()方法)

    JS图片的上传预览和表单提交(FileReader()方法) 一开始没有搞明白下面这块代码的,今天有时间简单整理下 核心点:FileReader()方法 以下是代码(以JSP文件为例) <!DO ...

  5. PHP 多图上传,图片批量上传插件,webuploader.js,百度文件上传插件

    PHP  多图上传,图片批量上传插件,webuploader.js,百度文件上传插件(案例教程) WebUploader作用:http://fex.baidu.com/webuploader/gett ...

  6. xhEditor实现ctrl+v粘贴word图片并上传

    自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了.一般情况下我们将Word内容粘贴到Web编辑器(富文本编辑器) ...

  7. asp.net实现图片在线上传并在线裁剪

    1.说明 接上一篇文章uploadify实现多附件上传完成后,又突然用到头像上传并在线裁剪.在网上找个众多例子都没有符合要求的,有一篇文章写的不错,就是文旺老兄写的这篇Asp.Net平台下的图片在线裁 ...

  8. 三款不错的图片压缩上传插件(webuploader+localResizeIMG4+LUploader)

    涉及到网页图片的交互,少不了图片的压缩上传,相关的插件有很多,相信大家都有用过,这里我就推荐三款,至于好处就仁者见仁喽: 1.名气最高的WebUploader,由Baidu FEX 团队开发,以H5为 ...

  9. Html5+asp.net mvc 图片压缩上传

    在做图片上传时,大图片如果没有压缩直接上传时间会非常长,因为有的图片太大,传到服务器上再压缩太慢了,而且损耗流量. 思路是将图片抽样显示在canvas上,然后用通过canvas.toDataURL方法 ...

随机推荐

  1. 记一次全站升级https引发的一系列问题

    中秋假期,闲来无事.花了一下午折腾了下https,说实话这年头还有网站不上https显然是折腾精神不够啊~ 1.SSL证书评估 看了市面上各种类型的证书,有收费的也有免费的,但是最终还是选择了腾讯云提 ...

  2. 使用Fiddler实现网络限速

    Fiddler实现网络限速方法: 1.点击FiddlerScript 2.在脚本里相应的地方添加“2”处两行代码(不加注释),保存(Save Script) 第一行为请求延时3秒,第二行为响应延时1. ...

  3. 我的第一个C程序

    // // main.c // one // // Created by Shuang Gai on 2019/1/19. // Copyright © 2019 Shuang Gai. All ri ...

  4. FoxPro 常用内部函数

    1.数学函数(数值函数) 求绝对值函数ABS 格式:ABS( expN) 求整函数INT 格式:INT( expN) 四舍五入函数ROUND 格式:ROUND( expN,〈保留小数位〉) 功能:按保 ...

  5. IMMDevice::Activate and specifying IID_IBaseFilter as the interface identifier

    Use the IMMDevice pointer to the endpoint returned by the enumeration process to activate the desire ...

  6. 迷你MVVM框架 avalonjs 学习教程2、模块化、ViewModel、作用域

    一个项目是由许多人分工写的,因此必须要合理地拆散,于是有了模块化.体现在工作上,PM通常它这为某某版块,某某频道,某某页面.某一个模块,必须是包含其固有的数据,样式,HTML与处理逻辑.在jQuery ...

  7. 再谈C#编码规范

    编码规范是老生常谈的问题,现在再看代码规范可能不会再去在意变量,控件的命名方法等,而是更加关注代码的实用性. 首先我们要明白一下几点, 1.代码写出来除了让他跑起来还有个非常非常重要的作用是维护,因为 ...

  8. 关于Url传递参数

    Url传递参数时,后台取值会直接取等号后面的内容,包括引号在内. 比如:   http://localhost:8080/user?name='admin' 这是错误的做法,后台获取到的参数是包括引号 ...

  9. 深入理解Spring的容器内事件发布监听机制

    目录 1. 什么是事件监听机制 2. JDK中对事件监听机制的支持 2.1 基于JDK实现对任务执行结果的监听 3.Spring容器对事件监听机制的支持 3.1 基于Spring实现对任务执行结果的监 ...

  10. react-navigation 3.x版本的安装以及react-native-gesture-handler配置

    一.安装依赖,使用npm或yarn命令,3.x版本必须安装react-native-gesture-handler react-navigation react-native-gesture-hand ...