asp.net mvc 3.0 动态无损图片压缩,及路由定义
2012-07-06 22:41:47 来源:WEB开发网EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
try
{
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)
{
ob.Save(stream, jpegICIinfo, ep);//jpegICIinfo是压缩后的新路径
}
else
{
ob.Save(stream, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
//iSource.Dispose();
ob.Dispose();
}
}
/// <summary>
/// 无损压缩图片
/// </summary>
/// <param name="sourceFile">原图片</param>
/// <param name="targetFile">压缩后保存位置</param>
/// <param name="height">高度</param>
/// <param name="width"></param>
/// <param name="quality">压缩质量 1-100</param>
/// <param name="type">压缩缩放类型</param>
/// <returns></returns>
public static bool Thumbnail(string sourceFile, string targetFile, int width, int height, int quality, ImgThumbnailType type)
{
ImageFormat tFormat = null;
Bitmap ob = Thumbnail(sourceFile, width, height, quality, type, out tFormat);
//以下代码为保存图片时,设置压缩质量
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = quality;//设置压缩的比例1-100
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
try
{
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)
{
ob.Save(targetFile, jpegICIinfo, ep);//jpegICIinfo是压缩后的新路径
}
else
{
ob.Save(targetFile, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
//iSource.Dispose();
ob.Dispose();
}
}
#endregion
}
}
4.测试运行
在看一下路由定义:
"{controller}/{action}/{width}/{height}/{root}/{type}/{pid}/{imgName}/"
和控制器中的方法:
public string Ebms(string width,string height,string root,string type,string pid,string imgName)
参数列表命名相同!
- ››ASP.NET及JS中的cookie基本用法
- ››ASP.NET获取MS SQL Server安装实例
- ››asp.net实现调用百度pai 在线翻译英文转中文
- ››ASP.NET页面选项进行提示判断
- ››Asp.net定时执行程序
- ››ASP.NET中利用DataList实现图片无缝滚动
- ››ASP.NET验证控件RequiredFieldValidator
- ››ASP.NET中使用System.Net.Mail发邮件
- ››ASP.NET中获取用户控件中控件的ID
- ››ASP.NET中FileBytes写成文件并存档
- ››asp获取毫秒数
- ››asp.net报“INSERT INTO 语句的语法错误解决
更多精彩
赞助商链接