Asp.net mvc实时生成缩率图到硬盘
2016-05-10 13:16
295 查看
///<summary>
///生成图片缩率图Action
///</summary>
///<paramname="p">原图url</param>
///<paramname="id">图片尺寸以及生成缩率图的类型</param>
///<returns></returns>
[ValidateInput(
false
)]
public
ActionResultIndex(
string
p,
string
id)
{
if
(
string
.IsNullOrEmpty(p))
{
return
new
HttpStatusCodeResult(404);
}
string
oPath=Regex.Replace(p,
@"http[s]?://(.*?)/"
,
"/"
,RegexOptions.IgnoreCase);
int
?oWidth=200,oHeight=200;
int
cutMode=3;
string
pPath;
string
oDir;
if
(!
string
.IsNullOrEmpty(id))
{
string
[]ss=id.Split(
new
char
[]{
'_'
},StringSplitOptions.RemoveEmptyEntries);
if
(ss.Length<2)
{
return
new
HttpStatusCodeResult(404);
}
if
(ss.Length>2)
{
cutMode=
int
.Parse(ss[2]);
}
oPath=oPath.Insert(oPath.LastIndexOf(
'/'
)+1,
string
.Format(
"{0}_{1}_{2}_"
,ss[0],ss[1],cutMode));
oWidth=
int
.Parse(ss[0]);
oHeight=
int
.Parse(ss[1]);
}
pPath=Server.MapPath(oPath);
oDir=Path.GetDirectoryName(pPath);
if
(!System.IO.File.Exists(pPath))
{
byte
[]imagebytes=FileHelper.DownLoadFile(p);
if
(!Directory.Exists(oDir))
{
Directory.CreateDirectory(oDir);
}
FileHelper.MakeThumbnail(FileHelper.BytToImg(imagebytes),oWidth.Value,oHeight.Value,(ThumbnailMode)cutMode,pPath,
true
);
}
return
File(pPath,FileHelper.GetContentTypeByExtension(Path.GetExtension(pPath).ToLower()));
}
访问方式:
http://www.souji8.com/Home/Index/{width}_{height}_{ThumMode}?p={imageUrl}
{imageUrl}:目标图片地址
{ThumMode}:1:指定高宽按比例、2:指定宽,高按比例、3:指定高宽裁减(不变形)
{Width}:期望图片宽
{Height}:期望图片高
相关文章推荐
- 使用OWIN 构建自宿主ASP.NET Web API 2
- 简单倒计时
- ASP.NET常用在线编辑器使用方法总结(二)
- Asp.net后台调用前台JS函数实现
- ASP.NET解决方案 添加现有文件夹到项目解决方案
- 【ASP.NET 插件】分享一款富文本web编辑器UEditor
- Asp.net中ListBox的用法!
- 在ASP.NET 2.0中操作数据之三十五:使用Repeater和DataList单页面实现主/从报表
- 提高 ASP.NET Web 应用性能的 24 种方法和技巧
- 在ASP.NET 2.0中操作数据之三十五:使用Repeater和DataList单页面实现主/从报表
- 菜刀ASP 新建文件抓包
- 菜刀ASP 新建文件夹抓包
- 彩票项目开发节项
- 菜刀ASP 修改文件时间名抓包
- ASP.NET mvc异常处理的方法
- IIS8集成模式下打开静态资源被aspx处理程序处理,StaticFileModule失效问题分析
- ASP.NET Core的配置(5):配置的同步[ 实例篇]
- 菜刀ASP 文件重命名抓包
- 菜刀ASP 删除文件抓包
- ASP.NET MVC 利用Razor引擎生成静态页