最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net生成缩略图并支持文件上传实现代码
时间:2011-11-24 编辑:简简单单 来源:一聚教程网
代码如下 | 复制代码 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Drawing; using System.IO; namespace web三层 { /// /// 显示请求图片的缩略图,以宽度100像素为最大单位 /// public class imgSmall : IHttpHandler { //图片所在文件夹 static string picturesPath = @"d:wordpictures"; public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/jpeg"; //获取到传递过来的img字符串,比如 //http://localhost:5002/imgSmall.ashx?img=abacus8.jpg这种 string img = context.Request.Params["img"]; string path = picturesPath + img; //如果文件存在才会去读取,减少使用try,catch,提高程序性能 if (File.Exists(path)) { //载入这个图片 Image big = Image.FromFile(path); //如果可以获取到文件,才会执行下面的代码 if (big != null) { //设定最大的宽度,可以修改来生成更小的缩略图 int newWidth = 100; //根据图片的宽高比来生成一个位图 Bitmap bitmap = new Bitmap(newWidth, newWidth * big.Height / big.Width); //根据图板来创建一个图画 Graphics g = Graphics.FromImage(bitmap); using (g) { //将大图big画到自己定义的小图中bitmap g.DrawImage(big, 0, 0, bitmap.Width, bitmap.Height); //直接将处理好的位图保存到响应输出流中,格式为jpeg! bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); } } } else { //否则就发送一个文件不存在的信息到浏览器 context.Response.ContentType = "text/html"; context.Response.Write("文件不存在"); //或者发送一个文件不存在的图片 //context.Response.WriteFile("todo此处修改为图片所在路径"); } } public bool IsReusable { get { return false; } } } } |
实例,可以把图片上传后再生成缩略图
代码如下 | 复制代码 |
请正确填写下面各项注意:
|
-
上一个: asp.net生成数据饼图实现代码
-
下一个: C#创建写入文件文件程序代码
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10