refer this code
public string GetConnectionString()
{
//sets the connection string from your web config file "ConnString" is the name of your Connection String
return System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
}
public void ProcessRequest(HttpContext context)
{
string id = context.Request.QueryString["id"]; //get the querystring value that was pass on the ImageURL (see GridView MarkUp in Page1.aspx)
if (id != null)
{
MemoryStream memoryStream = new MemoryStream();
SqlConnection connection = new SqlConnection(GetConnectionString());
string sql = "SELECT * FROM TblImages WHERE Id = @id";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddWithValue("@id", id);
connection.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
//Get Image Data
byte[] file = (byte[])reader["Image"];
reader.Close();
connection.Close();
memoryStream.Write(file, 0, file.Length);
context.Response.Buffer = true;
context.Response.BinaryWrite(file);
memoryStream.Dispose();
}
}
public bool IsReusable {
get {
return false;
}
}
http://geekswithblogs.net/dotNETvinz/archive/2009/04/24/displaying-image-to-image-control-based-on-user-selection-in.aspx