动态生成缩略图
(修改自 http://community.csdn.net//Expert/TopicView2.asp?id=4370310 中所帖的代码,版权没有)
Util.cs 的部分代码:
专门输出缩略图的页面 Thumbnail.aspx.cs 代码:
调用方法:
相对地址:
Thumbnail.aspx?width=200&height=300&src=upload/test.jpg
绝对地址:
Thumbnail.aspx?width=200&height=300&src=http://www.test.com/upload/test.jpg
Util.cs 的部分代码:
1
/// <summary>
2
/// 创建缩略图
3
/// </summary>
4
/// <param name="src">来源页面
5
/// 可以是相对地址或者绝对地址
6
/// </param>
7
/// <param name="width">缩略图宽度</param>
8
/// <param name="height">缩略图高度</param>
9
/// <returns>字节数组</returns>
10
public static byte[] MakeThumbnail(string src, double width, double height)
11
{
12
Image image;
13
14
// 相对路径从本机直接读取
15
if (src.ToLower().IndexOf("http://") == -1)
16
{
17
src = HttpContext.Current.Server.MapPath(src);
18
image = Image.FromFile(src, true);
19
}
20
else // 绝对路径从 Http 读取
21
{
22
HttpWebRequest req = (HttpWebRequest) WebRequest.Create(src);
23
req.Method = "GET";
24
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
25
Stream receiveStream = resp.GetResponseStream();
26
image = Image.FromStream(receiveStream);
27
resp.Close();
28
receiveStream.Close();
29
}
30
double newWidth, newHeight;
31
if (image.Width > image.Height)
32
{
33
newWidth = width;
34
newHeight = image.Height*(newWidth/image.Width);
35
}
36
else
37
{
38
newHeight = height;
39
newWidth = (newHeight/image.Height)*image.Width;
40
}
41
if (newWidth > width)
42
{
43
newWidth = width;
44
}
45
if (newHeight > height)
46
{
47
newHeight = height;
48
}
49
//取得图片大小
50
Size size = new Size((int) newWidth, (int) newHeight);
51
//新建一个bmp图片
52
Image bitmap = new Bitmap(size.Width, size.Height);
53
//新建一个画板
54
Graphics g = Graphics.FromImage(bitmap);
55
//设置高质量插值法
56
g.InterpolationMode = InterpolationMode.High;
57
//设置高质量,低速度呈现平滑程度
58
g.SmoothingMode = SmoothingMode.HighQuality;
59
//清空一下画布
60
g.Clear(Color.White);
61
//在指定位置画图
62
g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height),
63
new Rectangle(0, 0, image.Width, image.Height),
64
GraphicsUnit.Pixel);
65
///文字水印
66
//System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
67
//System.Drawing.Font f=new Font("宋体",10);
68
//System.Drawing.Brush b=new SolidBrush(Color.Black);
69
//G.DrawString("myohmine",f,b,10,10);
70
//G.Dispose();
71
///图片水印
72
//System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
73
//Graphics a = Graphics.FromImage(bitmap);
74
//a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
75
//copyImage.Dispose();
76
//a.Dispose();
77
//copyImage.Dispose();
78
//保存高清晰度的缩略图
79
MemoryStream stream = new MemoryStream();
80
bitmap.Save(stream, ImageFormat.Jpeg);
81
byte[] buffer = stream.GetBuffer();
82
g.Dispose();
83
image.Dispose();
84
bitmap.Dispose();
85
return buffer;
86
}
/// <summary>2
/// 创建缩略图3
/// </summary>4
/// <param name="src">来源页面5
/// 可以是相对地址或者绝对地址6
/// </param>7
/// <param name="width">缩略图宽度</param>8
/// <param name="height">缩略图高度</param>9
/// <returns>字节数组</returns>10
public static byte[] MakeThumbnail(string src, double width, double height)11
{12
Image image;13

14
// 相对路径从本机直接读取15
if (src.ToLower().IndexOf("http://") == -1)16
{17
src = HttpContext.Current.Server.MapPath(src);18
image = Image.FromFile(src, true);19
}20
else // 绝对路径从 Http 读取21
{22
HttpWebRequest req = (HttpWebRequest) WebRequest.Create(src);23
req.Method = "GET";24
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();25
Stream receiveStream = resp.GetResponseStream();26
image = Image.FromStream(receiveStream);27
resp.Close();28
receiveStream.Close();29
}30
double newWidth, newHeight;31
if (image.Width > image.Height)32
{33
newWidth = width;34
newHeight = image.Height*(newWidth/image.Width);35
}36
else37
{38
newHeight = height;39
newWidth = (newHeight/image.Height)*image.Width;40
}41
if (newWidth > width)42
{43
newWidth = width;44
}45
if (newHeight > height)46
{47
newHeight = height;48
}49
//取得图片大小50
Size size = new Size((int) newWidth, (int) newHeight);51
//新建一个bmp图片52
Image bitmap = new Bitmap(size.Width, size.Height);53
//新建一个画板54
Graphics g = Graphics.FromImage(bitmap);55
//设置高质量插值法56
g.InterpolationMode = InterpolationMode.High;57
//设置高质量,低速度呈现平滑程度58
g.SmoothingMode = SmoothingMode.HighQuality;59
//清空一下画布60
g.Clear(Color.White);61
//在指定位置画图62
g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height),63
new Rectangle(0, 0, image.Width, image.Height),64
GraphicsUnit.Pixel);65
///文字水印66
//System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);67
//System.Drawing.Font f=new Font("宋体",10);68
//System.Drawing.Brush b=new SolidBrush(Color.Black);69
//G.DrawString("myohmine",f,b,10,10);70
//G.Dispose();71
///图片水印72
//System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));73
//Graphics a = Graphics.FromImage(bitmap);74
//a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);75
//copyImage.Dispose();76
//a.Dispose();77
//copyImage.Dispose();78
//保存高清晰度的缩略图79
MemoryStream stream = new MemoryStream();80
bitmap.Save(stream, ImageFormat.Jpeg);81
byte[] buffer = stream.GetBuffer();82
g.Dispose();83
image.Dispose();84
bitmap.Dispose();85
return buffer;86
}专门输出缩略图的页面 Thumbnail.aspx.cs 代码:
1
namespace JCDWeb
2
{
3
using System;
4
using System.Web.UI;
5
6
public class Thumbnail : Page
7
{
8
private void Page_Load(object sender, EventArgs e)
9
{
10
string src = GetQueryStringSrc();
11
double width = GetQueryStringWidth();
12
double height = GetQueryStringHeight();
13
14
Response.ContentType = "image/jpeg";
15
Response.Clear();
16
17
if (src.Length > 0 && width > 0 && height > 0)
18
{
19
try
20
{
21
byte[] buffer = Util.MakeThumbnail(src, width, height);
22
Response.BinaryWrite(buffer);
23
Response.Flush();
24
}
25
catch
26
{
27
28
}
29
}
30
}
31
32
handle query string
72
73
Web Forms Designer generated code
87
}
88
}
namespace JCDWeb2
{3
using System;4
using System.Web.UI;5

6
public class Thumbnail : Page7
{8
private void Page_Load(object sender, EventArgs e)9
{10
string src = GetQueryStringSrc();11
double width = GetQueryStringWidth();12
double height = GetQueryStringHeight();13

14
Response.ContentType = "image/jpeg";15
Response.Clear();16

17
if (src.Length > 0 && width > 0 && height > 0)18
{19
try 20
{21
byte[] buffer = Util.MakeThumbnail(src, width, height);22
Response.BinaryWrite(buffer);23
Response.Flush();24
}25
catch26
{27

28
}29
}30
}31

32
handle query string72

73
Web Forms Designer generated code87
}88
}调用方法:
相对地址:
Thumbnail.aspx?width=200&height=300&src=upload/test.jpg
绝对地址:
Thumbnail.aspx?width=200&height=300&src=http://www.test.com/upload/test.jpg


浙公网安备 33010602011771号