Thursday, April 23, 2009

Display images from database

Hi,

Now a days developer like to save images to database.

Now the problem how to get image back from database.

For the same purpose we can use HttpHandler which can get the image from database.

and can be applied easily :

Here is code for that

you has to create a new generic handler.say its name is ImageViewer.ashx

here you put the following code in ProcessRequest() of the httphandler

context.Request.QueryString["ImageID"].ToString() // id of the row of database

Stream stream = null;

context.Response.ContentType = "image/jpeg/gif";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;

int buffersize = 1024 * 16;
byte[] buffer = new byte[buffersize];
//Run your query here and get the binary stream from database assign it the stream variable as below
stream = stream from database;
int count = stream.Read(buffer, 0, buffersize);

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

----------------------- that all has to do in httphandler --------------
Now in the .aspx this what you need to do :







and you are done .

You can view the same here :

http://forums.asp.net/t/1394122.aspx?PageIndex=2

No comments:

Post a Comment