<%@ WebHandler Language=
"C#"
Class=
"ReadImage"
%>
using
System;
using
System.Web;
using
System.Data.SqlClient;
using
System.IO;
using
System.Configuration;
public
class
ReadImage : IHttpHandler
{
public
void
ProcessRequest (HttpContext context) {
Int32 theID;
if
(context.Request.QueryString[
"id"
] !=
null
)
theID = Convert.ToInt32(context.Request.QueryString[
"id"
]);
else
throw
new
ArgumentException(
"No parameter specified"
);
context.Response.ContentType =
"image/jpeg"
;
Stream strm = DisplayImage(theID);
byte
[] buffer =
new
byte
[2048];
int
byteSeq = strm.Read(buffer, 0, 2048);
while
(byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 2048);
}
}
public
Stream DisplayImage(
int
theID)
{
string
cs = ConfigurationManager.ConnectionStrings[
"TempConnectionString"
].ConnectionString;
using
( SqlConnection connection =
new
SqlConnection(cs))
{
string
sql =
"SELECT image FROM tblImage WHERE id = @ID"
;
using
(SqlCommand cmd =
new
SqlCommand(sql, connection))
{
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.AddWithValue(
"@ID"
, theID);
connection.Open();
object
theImg = cmd.ExecuteScalar();
try
{
return
new
MemoryStream((
byte
[])theImg);
}
catch
{
return
null
;
}
finally
{
cmd.Dispose();
connection.Close();
connection.Dispose();
}
}
}
}
public
bool
IsReusable {
get
{
return
false
;
}
}
}