Code reference self http://blog.joycode.com/liuhuimiao/archive/2005/06/03/52554.aspx? Pending = true
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
21
22 // setup the photoid Parameter
23 int32 id =-1;
24 stream = NULL;
25 if (context. Request. querystring ["photoid"]! = NULL & context. Request. querystring ["photoid"]! = ""){
26 id = convert. toint32 (context. Request. querystring ["photoid"]);
27 stream = (getphoto (ID ));
28} else {
29 return;
30}
31 // write image stream to the response stream
32 const int buffersize = 1024*16;
33 byte [] buffer = new byte [buffersize];
34 int COUNT = stream. Read (buffer, 0, buffersize );
35 while (count> 0 ){
36 context. response. outputstream. Write (buffer, 0, count );
37 COUNT = stream. Read (buffer, 0, buffersize );
38}
39}
40 public stream getphoto (INT photoid)
41 {
42 sqlconnection myconnection = new sqlconnection (configurationmanager. connectionstrings ["personal"]. connectionstring );
43 sqlcommand mycommand = new sqlcommand ("select [Photo] from [employees] Where [employeeid] = @ employeeid", myconnection );
44 mycommand. commandtype = commandtype. text;
45 mycommand. Parameters. Add (New sqlparameter ("@ employeeid", photoid ));
46 myconnection. open ();
47 object result = mycommand. executescalar ();
48
49 try
50 {
51 return New memorystream (byte []) result );
52}
53 catch (argumentnullexception E)
54 {
55 return NULL;
56}
57 finally
58 {
59 myconnection. Close ();
60}
61}
62
63}