时间:2016-02-15 22:08 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【asp.net生成缩略图实现代码】,下面是详细的讲解!
asp.net生成缩略图实现代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;
namespace web三层
{
/// <summary>
/// 显示请求图片的缩略图,以宽度100像素为最大单位
/// </summary>
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生成缩略图实现代码的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【asp】asp.net ubb使用代码-net-ubb使用
- 【默认图片】图片不存在使用默认图片代替的实例
- 【页面打印】关于ASP.NET页面打印技术的常用方法
- 【MVC5】MVC 5 第一章 创建MVC 5 web应用程序-net
- 【MVC】一个简单MVC5 + EF6示例分享-EF6实例-MVC5
- 【服务器】asp.net页面状态管理cookie和服务器状
- 【asp】asp.net更新指定记录的方法-net--更新-指
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
