img src="Handler.ashx?PhotoID=<%# Eval("PhotoID") %>&Size=S"
很奇怪的事情,只是发现了,但不明白为什么!

Handler
1
<%@ WebHandler Language="C#" Class="Handler" %>
2
3
using System;
4
using System.IO;
5
using System.Web;
6
7
public class Handler : IHttpHandler
{
8
9
public bool IsReusable
{
10
get
{
11
return true;
12
}
13
}
14
15
public void ProcessRequest (HttpContext context)
{
16
// Set up the response settings
17
context.Response.ContentType = "image/jpeg";
18
context.Response.Cache.SetCacheability(HttpCacheability.Public);
19
context.Response.BufferOutput = false;
20
// Setup the Size Parameter
21
PhotoSize size;
22
switch (context.Request.QueryString["Size"])
{
23
case "S":
24
size = PhotoSize.Small;
25
break;
26
case "M":
27
size = PhotoSize.Medium;
28
break;
29
case "L":
30
size = PhotoSize.Large;
31
break;
32
default:
33
size = PhotoSize.Original;
34
break;
35
}
36
// Setup the PhotoID Parameter
37
Int32 id = -1;
38
Stream stream = null;
39
if (context.Request.QueryString["PhotoID"] != null && context.Request.QueryString["PhotoID"] != "")
{
40
id = Convert.ToInt32(context.Request.QueryString["PhotoID"]);
41
stream = PhotoManager.GetPhoto(id, size);
42
} else
{
43
id = Convert.ToInt32(context.Request.QueryString["AlbumID"]);
44
stream = PhotoManager.GetFirstPhoto(id, size);
45
}
46
// Get the photo from the database, if nothing is returned, get the default "placeholder" photo
47
if (stream == null) stream = PhotoManager.GetPhoto(size);
48
// Write image stream to the response stream
49
const int buffersize = 1024 * 16;
50
byte[] buffer = new byte[buffersize];
51
int count = stream.Read(buffer, 0, buffersize);
52
while (count > 0)
{
53
context.Response.OutputStream.Write(buffer, 0, count);
54
count = stream.Read(buffer, 0, buffersize);
55
}
56
}
57
58
}
据说是能以流的方式直接读取存在数据库中的图片。
1
<%@ WebHandler Language="C#" Class="Handler" %>2

3
using System;4
using System.IO;5
using System.Web;6

7

public class Handler : IHttpHandler
{8

9

public bool IsReusable
{10

get
{11
return true;12
}13
}14
15

public void ProcessRequest (HttpContext context)
{16
// Set up the response settings17
context.Response.ContentType = "image/jpeg";18
context.Response.Cache.SetCacheability(HttpCacheability.Public);19
context.Response.BufferOutput = false;20
// Setup the Size Parameter21
PhotoSize size;22

switch (context.Request.QueryString["Size"])
{23
case "S":24
size = PhotoSize.Small;25
break;26
case "M":27
size = PhotoSize.Medium;28
break;29
case "L":30
size = PhotoSize.Large;31
break;32
default:33
size = PhotoSize.Original;34
break;35
} 36
// Setup the PhotoID Parameter37
Int32 id = -1;38
Stream stream = null;39

if (context.Request.QueryString["PhotoID"] != null && context.Request.QueryString["PhotoID"] != "")
{40
id = Convert.ToInt32(context.Request.QueryString["PhotoID"]);41
stream = PhotoManager.GetPhoto(id, size);42

} else
{43
id = Convert.ToInt32(context.Request.QueryString["AlbumID"]);44
stream = PhotoManager.GetFirstPhoto(id, size);45
}46
// Get the photo from the database, if nothing is returned, get the default "placeholder" photo47
if (stream == null) stream = PhotoManager.GetPhoto(size);48
// Write image stream to the response stream49
const int buffersize = 1024 * 16;50
byte[] buffer = new byte[buffersize];51
int count = stream.Read(buffer, 0, buffersize);52

while (count > 0)
{53
context.Response.OutputStream.Write(buffer, 0, count);54
count = stream.Read(buffer, 0, buffersize);55
}56
}57

58
}

浙公网安备 33010602011771号