点击这里给我发消息 点击这里给我发消息

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

添加时间:2013-12-6
    相关阅读: 相关内容

 1.定义路由

2.编写控制器

3.编写图片压缩方法

4.测试运行

---------------------------------------------------

1.定义路由 ,一般写在 Globals.cs 文件中

public static void RegisterRoutes(RouteCollection routes)
{

////~/images/XiaZhuang/649500/test.jpg
routes.MapRoute(
"Default12", // 图片路由名称
"{controller}/{action}/{width}/{height}/{root}/{type}/{pid}/{imgName}/", // 带有参数的 URL
new { controller = "EBMS", action = "Index", id = UrlParameter.Optional }, // 参数默认值
new string[] { "EB.Ctrl.Controllers" });
}
2.编写控制器

public class ImagesController : Controller
{
/// <summary>
/// 图片压缩处理
/// </summary>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <param name="root">根节目录</param>
/// <param name="type">类型</param>
/// <param name="pid">商品ID</param>
/// <param name="imgName">图片名称</param>
/// <returns></returns>
public string Ebms(string width,string height,string root,string type,string pid,string imgName)
{
string sourceFile = string.Format("~/{0}/{1}/{2}/{3}"
, root
, type
, pid
, imgName);

sourceFile = Server.MapPath(sourceFile);
if (!System.IO.File.Exists(sourceFile))
return string.Format("图片路径无效:{0}",sourceFile);

EB.Sys.Images.ImgThumbnail.Thumbnail(
sourceFile
, Response.OutputStream
, width.GetInt()
, height.GetInt()
, 90
, Sys.Images.ImgThumbnail.ImgThumbnailType.W);
//测试输出流中是否存在内容
//byte[] arr = new byte[1000];
//Response.OutputStream.Write(arr,0,arr.Length);
return string.Empty;
}
}

3.编写图片压缩方法

这个方法在我另外一篇文章中有介绍,这里做了个加工

http://blog.csdn.net/xxj_jing/article/details/7715729

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace EB.Sys.Images
{
/// <summary>
/// 图片压缩
/// </summary>
public class ImgThumbnail
{
/// <summary>
/// 指定缩放类型
/// </summary>
public enum ImgThumbnailType
{
/// <summary>
/// 指定高宽缩放(可能变形)
/// </summary>
WH = 0,
/// <summary>
/// 指定宽,高按比例
/// </summary>
W = 1,
/// <summary>
/// 指定高,宽按比例
/// </summary>
H = 2,
/// <summary>
/// 指定高宽裁减(不变形)
/// </summary>
Cut = 3
}
#region Thumbnail
/// <summary>
/// 无损压缩图片
/// </summary>
/// <param name="sourceFile">原图片</param>
/// <param name="stream">压缩后保存到流中</param>
/// <param name="height">高度</param>
/// <param name="width"></param>
/// <param name="quality">压缩质量 1-100</param>
/// <param name="type">压缩缩放类型</param>
/// <returns></returns>
private static Bitmap Thumbnail(string sourceFile, int width, int height, int quality, ImgThumbnailType type, out ImageFormat tFormat)
{
using (System.Drawing.Image iSource = System.Drawing.Image.FromFile(sourceFile))
{
tFormat = iSource.RawFormat;
//缩放后的宽度和高度
int towidth = width;
int toheight = height;
//
int x = 0;
int y = 0;
int ow = iSource.Width;
int oh = iSource.Height;

switch (type)
{
case ImgThumbnailType.WH://指定高宽缩放(可能变形)
{
break;
}
case ImgThumbnailType.W://指定宽,高按比例
{
toheight = iSource.Height * width / iSource.Width;
break;
}
case ImgThumbnailType.H://指定高,宽按比例
{
towidth = iSource.Width * height / iSource.Height;
break;
}
case ImgThumbnailType.Cut://指定高宽裁减(不变形)
{
if ((double)iSource.Width / (double)iSource.Height > (double)towidth / (double)toheight)
{
oh = iSource.Height;
ow = iSource.Height * towidth / toheight;
y = 0;
x = (iSource.Width - ow) / 2;
}
else
{
ow = iSource.Width;
oh = iSource.Width * height / towidth;
x = 0;
y = (iSource.Height - oh) / 2;
}
break;
}
default:
break;
}

Bitmap ob = new Bitmap(towidth, toheight);
Graphics g = Graphics.FromImage(ob);
g.Clear(System.Drawing.Color.WhiteSmoke);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(iSource
, new Rectangle(x, y, towidth, toheight)
, new Rectangle(0, 0, iSource.Width, iSource.Height)

, GraphicsUnit.Pixel);
g.Dispose();

return ob;

}
}
/// <summary>
/// 无损压缩图片
/// </summary>
/// <param name="sourceFile">原图片</param>
/// <param name="stream">压缩后保存到流中</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, System.IO.Stream stream, 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(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)

参数列表命名相同!

 

 


 

咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册