首页 > 编程语言 >asp.net mvc 3.0 动态无损图片压缩,及路由定义

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

时间:2022-11-03 22:33:01浏览次数:41  
标签:asp string int 压缩 iSource height width mvc 3.0


最新更新:120821

1.定义路由

2.编写控制器

3.编写图片压缩方法

4.测试运行

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

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

routes.MapRoute(
"ImgRoute", // 图片路由名称
"pics/{thumbnailType}/{width}/{height}/{*imgPath}", // 带有参数的 URL
new { controller = "Pictures", action = "ZoomImg", id = UrlParameter.Optional }, // 参数默认值
nameSpace);

2.编写控制器

public class ImagesController : Controller
{
/// <summary>
/// 缩放图片
/// </summary>
/// <param name="thumbnailType">图片缩放方式(取值参考:EB.Sys.Images.ImgThumbnail.ImgThumbnailType 枚举值)</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <param name="imgPath">图片路径</param>
/// <returns></returns>
public string ZoomImg(string thumbnailType, string width, string height, string imgPath)
{
//return string.Format("{0} : {1} , {2} , {3} , {4}"
// ,DateTime.Now
// ,thumbnailType
// ,width
// ,height
// ,imgPath); string sourceFile = Server.MapPath(string.Format("~/{0}", imgPath));
if (!System.IO.File.Exists(sourceFile))
return string.Format("图片路径无效:{0}", sourceFile); ImgThumbnail.ImgThumbnailType type = (ImgThumbnail.ImgThumbnailType)thumbnailType.GetInt();
if (type == ImgThumbnail.ImgThumbnailType.Nothing)
return string.Format("压缩类型错误:{0}", thumbnailType); EB.Sys.Images.ImgThumbnail.Thumbnail(
sourceFile
, Response.OutputStream
, width.GetInt()
, height.GetInt()
, 90
, type);
//测试输出流中是否存在内容
//byte[] arr = new byte[1000];
//Response.OutputStream.Write(arr,0,arr.Length);
return string.Empty;
} }

 

3.编写图片压缩方法

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

 

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;namespace EB.Sys.Images
{
/// <summary>
/// 图片压缩
/// </summary>
public class ImgThumbnail
{
#region 枚举
/// <summary>
/// 指定缩放类型
/// </summary>
public enum ImgThumbnailType
{
/// <summary>
/// 无
/// </summary>
Nothing=0,
/// <summary>
/// 指定高宽缩放(可能变形)
/// </summary>
WH = 1,
/// <summary>
/// 指定宽,高按比例
/// </summary>
W = 2,
/// <summary>
/// 指定高,宽按比例
/// </summary>
H = 3,
/// <summary>
/// 指定高宽裁减(不变形)
/// </summary>
Cut = 4,
/// <summary>
/// 按照宽度成比例缩放后,按照指定的高度进行裁剪
/// </summary>
W_HCut = 5,
}
#endregion #region 图片压缩
/// <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 oWidth = iSource.Width;
int oHeight = 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)
{
oHeight = iSource.Height;
oWidth = iSource.Height * toWidth / toHeight;
y = 0;
x = (iSource.Width - oWidth) / 2;
}
else
{
oWidth = iSource.Width;
oHeight = iSource.Width * height / toWidth;
x = 0;
y = (iSource.Height - oHeight) / 2;
}
break;
}
case ImgThumbnailType.W_HCut://按照宽度成比例缩放后,按照指定的高度进行裁剪
{
toHeight = iSource.Height * width / iSource.Width;
if (height < toHeight)
{
oHeight = oHeight * height / toHeight;
toHeight = toHeight * height / toHeight;
}
break;
}
default:
break;
} Bitmap ob = new Bitmap(toWidth, toHeight);
//ImgWaterMark iwm = new ImgWaterMark();
//iwm.AddWaterMark(ob, towidth, toheight, "​​​www.***.com​​​");
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, oWidth, oHeight)
, 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);
//水印
ImgWaterMark.AddWaterMark(ob, "​​​www.***.com​​​");
//以下代码为保存图片时,设置压缩质量
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.测试运行

www.****.com/pics/5/195/395/images/ShangZhuang/610505/610505_00--w_498_h_498.jpg

在看一下路由定义:

 " pics/{thumbnailType}/{width}/{height}/{*imgPath}"

 {thumbnailType} 匹配 5

 {width} 匹配 195

{height} 匹配 395

{*imgPath} 匹配 images/ShangZhuang/610505/610505_00--w_498_h_498.jpg

和控制器中的方法:

  public string ZoomImg(string thumbnailType, string width, string height, string imgPath)

你明白的!参数列表命名相同!

 

 

标签:asp,string,int,压缩,iSource,height,width,mvc,3.0
From: https://blog.51cto.com/xxjjing/5821275

相关文章

  • C# .Net MVC Razor 视图静态分页导航栏生成器
    这里贴出3个重要组成部分1.分页导航栏主算法代码(包括@Html扩展方法)2.HTML页面调用代码(包括导航栏样式)3.Controller控制器代码4.测试路径:http://www.****.com/EBusines......
  • Web3.0 - 比特币
    比特币是区块链技术的一个典型应用。央行发行的数字货币,由银行提供验证、背书。比特币是一个去中心化的数字货币。去中心化的数字货币,需要解决的问题是:谁发行货币怎么......
  • SpringMVC源码-创建RequestMappingHandlerAdapter
    一、RequestMappingHandlerAdapterRequestMappingHandlerAdapter所属BeanDifinition的属性。RequestMappingHandlerAdapter是将当前请求适配到@RequestMapping类型的Ha......
  • SpringMVC中的@RestController
    在Spring中@RestController的作用等同于@Controller+@ResponseBody。所以想要理解@RestController注解就要先了解@Controller和@ResponseBody注解。@Controller之前已......
  • SpringMVC中Controller的运用
    Controller的运用通常通过接口和注解的方式实现第一种:实现Controller接口,Controller是一个接口,在org.springframework.web.servlet.mvc包下,接口中只有一个方法;编写一个Co......
  • notepad++搭建gtk3.0/2.0环境_F_hawk189_新浪博客
    准备下载以下内容​​notepad++​​​​mingw​​(包含msys和gcc工具链)​​gtk+bundle​​(2.x或3.x都可以,不过现在官网都是使用msys下载)安装......
  • springmvc-handlerInterceptor
    HandlerInterceptor接口给我们提供了3个方法:(1)preHandle:在执行controller处理之前执行,返回值为boolean,返回值为true时接着执行postHandle和afterCompletion,如果我们返......
  • 安装Mariadb-10.6.10 需要升级CMake 3.0 以上版本
    下载wgethttps://cmake.org/files/v3.22/cmake-3.22.6.tar.gz安装tarzxvfcmake-3.22.6.tar.gzcdcmake-3.22.6./bootstrapgmakegmakeinstall注意安装完后......
  • 使用MVC实现登录注册功能(数据保存到数据库)详细讲解以及代码
    M:代表模型层,解决问题的功能具体实现。比如向数据库添加数据、查询数据V:代表视图,用户和机器的交互页面。用来展示信息(一般用html,js,css...)C:控制层,用来连接用户提交的操作......
  • Spring MVC的工作原理,我们来看看其源码实现
    开心一刻晚上陪老丈人吃饭,突然手机响了,我手贱按了免提……哥们:快出来喝酒!哥几个都在呢!我:今天不行,我现在陪老丈人吃饭呢哥们:那你抓紧喝,我三杯白酒,把我岳父放......