.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;
}
}
}
相关文章推荐
- Sublime2编译Python3程序EOFError:EOF when reading a line解决方法
- C++ 程序变量作用域生命周期笔记
- python 关闭 os.popen()
- 你应该更新的 Java 知识之常用程序库【转载】
- VC++的Unicode编程
- 国嵌C++专题3-八剑齐飞
- .whl文件打开方式 Python
- C++ 模板详解(一)(二)
- 小解C++模板特化
- JAVA面向对象第一天
- MVC——数据库增删改查(aspx)
- C++类模板的三种特化
- Python 的格式化字符串format函数
- Spring Mvc+ajax发送请求参数接收问题
- JAVA-反射-getGenericSuperclass()介绍
- PCA and kmeans MATLAB实现
- Java程序设计日志一
- C语言sizeof求结构体大小问题汇总
- MyBatis查询传一个参数时报错:There is no getter for property named 'sleevetype' in 'class java.lang.Integer
- Ruby on Rails Tutorial 第二章 之 用户资源&MVC&REST