WEB开发网
开发学院WEB开发ASP.NET asp.net mvc 3.0 动态无损图片压缩,及路由定义 阅读

asp.net mvc 3.0 动态无损图片压缩,及路由定义

 2012-07-06 22:41:47 来源:WEB开发网   
核心提示:qy[0] = quality;//设置压缩的比例1-100EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);ep.Param[0] = eParam;try{ImageCodecInfo
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(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)

参数列表命名相同!

上一页  1 2 3 

Tags:asp net mvc

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接