您的位置:首页 > 编程语言 > ASP

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}:期望图片高
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: