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

.net 缩略图代码

2015-07-05 21:38 429 查看

<body>

<!--enctype="multipart/form-data" 这个要加上-->

<form id="form1" action="load.ashx" method="post" enctype="multipart/form-data">

<div>

<input type="file" name="name" value=" " /><input type="submit" value="upload" />

</div>

</form>

</body>

后台:

public class load : IHttpHandler

{

public void ProcessRequest(HttpContext context)

{

context.Response.ContentType = "text/plain";

//接受用户上传的文件

HttpPostedFile fi = context.Request.Files[0];

//判断类型

string fileType = Path.GetExtension(fi.FileName);

switch (fileType)

{

case ".gif": LoadImg(fi,context,fileType); break;

case ".jpg": LoadImg(fi, context, fileType); break;

case ".jpeg": LoadImg(fi, context, fileType); break;

case ".png": LoadImg(fi, context, fileType); break;

default:

break;

}

context.Response.Write("ok");

}

private void LoadImg(HttpPostedFile f, HttpContext context, string fileType)

{

//根据上传的文件创建一个image对象 (原始img)

using (Image imgBig = Image.FromStream(f.InputStream))

{

int iBigWidth = imgBig.Width;

int iBigHeight = imgBig.Height;

//创建image对象(缩略图)

using (Image imgSmall = new Bitmap(200, 200 * iBigHeight / iBigWidth))

{

//基于缩略图创建画布

using (Graphics g = Graphics.FromImage(imgSmall))

{

//把大图画到缩略图上

g.DrawImage(imgBig, 0, 0, imgSmall.Width, imgSmall.Height);

}

//保存

imgBig.Save(context.Request.MapPath(Guid.NewGuid().ToString()+"B"+fileType));

imgSmall.Save(context.Request.MapPath(Guid.NewGuid().ToString() + "S" + fileType));

}

}

}

public bool IsReusable

{

get

{

return false;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: