How to show image in database in Asp.net control?

How to show image in database in Asp.net control? We need to show the employee image along with our data on the asp.net page, but the problem is how to display the image on the asp.net image control to control the image using ImageUrl image.

Kindly guide ....

0
source share
2 answers

You can create an HttpHandler (ashx) page that would execute the request and set this as the imageUrl property for image management

<asp:image id="imgEmployee" imageUrl="DisplayImage.ashx?employeeId=<someId>"/> 

Now in DisplayImage.ashx you can override ProcessRquest as shown below: -

  public void ProcessRequest (HttpContext context) { int employeeId; if (context.Request.QueryString["employeeId"] != null) employeeId = Convert.ToInt32(context.Request.QueryString["employeeId"]); else throw new ArgumentException("No parameter specified"); byte[] imageData= ;// get the image data from the database using the employeeId Querystring Response.ContentType = "image/jpeg"; // You can retrieve this also from the database Response.BinaryWrite(imageData); } 

Changes in Web.config: -

 <httpHandlers> <add verb="*" path="img/*" type="DisplayImage"/> </httpHandlers> 

More details here and here .

Hope this helps.

+4
source share

This can also be done without creating a handler.

 //get the image from the database as byte array byte[] image = (byte[])dr["image"]; //set the ImageUrl of the Image Control as a Base64 string Image1.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(image) 

Or, if you want c and height also, create an Image using a MemoryStream and get the image properties.

 using (MemoryStream ms = new MemoryStream(image)) { System.Drawing.Image imageFromDB = System.Drawing.Image.FromStream(ms); Image1.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(image); Image1.Width = imageFromDB.Width; Image1.Height = imageFromDB.Height; } 
0
source share

All Articles