In order to display an image stored in SQL Server on the Web Form, we need the concept of Generic Handler in ASP.NET. A Generic Handler is just any other web form that is added in the current form. A generic handler is responsible for fulfilling requests from a browser. A handler is a class that implements IHttpHandler interface. Normally the generic handler is having an extension of “Handler.ashx”.
In my previous post, I discussed about the storing of an image in SQL Server which the user selects on the web page (.aspx) page. I will be continuing from the same process. The following interface is used:
In the above screen shot, a textbox is there in which the Image id will be entered by the user which the user wants to display. Also the button will be used to show the same. And lastly an image control that will display the image fetched from the database.
On the Show Image button the following coding is to be done. The hander is invoked with the help of following code:
Protected void ShowImage_Click(object sender, EventArgs e)
{
Image1.ImageUrl = “Hander.ashx?id=”+TextBox1.Text;
}
Next Step is to add a generic handler from WebSite Menu and select Add new Item:
using System;
using System.Web;
using System.Data.SqlClient;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
String vImageID = context.Request.QueryString["id"];
SqlConnection vConn = new SqlConnection();
vConn.Open();
String vQuery = "Select * From ImageTable where id = @id";
SqlCommand vComm=new SqlCommand (vQuery,vConn);
vComm.Parameters.AddWithValue("@id",vImageID);
SqlDataReader myReader= vComm.ExecuteReader();
while(myReader.Read())
{
context.Response.ContentType="image/jpg";
context.Response.BinaryWrite((byte[])myReader["Photo"]);
}
myReader.Close();
vConn.Close();
}
public bool IsReusable {
get {
return false;
}
}
}
In the above coding, we are trying to get the ID of the image which the user has entered in the previous page. And then we are doing the following stuff:
1. Open the database connection.
2. Fire query which gets the image of the selected user id which the user has passed from the previous page.
3. Reading the image in the form of DataReader.
4. And ultimately writing the DataReader with the help of context.Response.BinaryWrite. When we use this we need to typecast in the format of byte[] array because the image is also stored in the format of bytes.
I hope this article will help you in displaying images on web form (.aspx) page that are fetched out from a database.
Have nice time....
In my previous post, I discussed about the storing of an image in SQL Server which the user selects on the web page (.aspx) page. I will be continuing from the same process. The following interface is used:
On the Show Image button the following coding is to be done. The hander is invoked with the help of following code:
Protected void ShowImage_Click(object sender, EventArgs e)
{
Image1.ImageUrl = “Hander.ashx?id=”+TextBox1.Text;
}
Next Step is to add a generic handler from WebSite Menu and select Add new Item:
using System;
using System.Web;
using System.Data.SqlClient;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
String vImageID = context.Request.QueryString["id"];
SqlConnection vConn = new SqlConnection(
vConn.Open();
String vQuery = "Select * From ImageTable where id = @id";
SqlCommand vComm=new SqlCommand (vQuery,vConn);
vComm.Parameters.AddWithValue("@id",vImageID);
SqlDataReader myReader= vComm.ExecuteReader();
while(myReader.Read())
{
context.Response.ContentType="image/jpg";
context.Response.BinaryWrite((byte[])myReader["Photo"]);
}
myReader.Close();
vConn.Close();
}
public bool IsReusable {
get {
return false;
}
}
}
In the above coding, we are trying to get the ID of the image which the user has entered in the previous page. And then we are doing the following stuff:
1. Open the database connection.
2. Fire query which gets the image of the selected user id which the user has passed from the previous page.
3. Reading the image in the form of DataReader.
4. And ultimately writing the DataReader with the help of context.Response.BinaryWrite. When we use this we need to typecast in the format of byte[] array because the image is also stored in the format of bytes.
I hope this article will help you in displaying images on web form (.aspx) page that are fetched out from a database.
Have nice time....
Comments
byte[] blob = (byte[])rd["PicImage"];
MemoryStream ms = new MemoryStream(blob);
ms.Write(blob, 0, (int)blob.Length);
Image1.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(ms.ToArray());