tags:

views:

134

answers:

2

How to show a image in database in the image control of Asp.net? We have to show the image of employee along with his details in the asp.net page, but the issue is how to show the image on the asp.net image control for the image control takes picture by the property ImageUrl.

Kindly guide....

A: 

The below link can hlp you. I have done the same task using the code reffered there

http://csharpdotnetfreak.blogspot.com/2009/07/display-images-gridview-from-database.html

Hojo
+1  A: 

You can create an HttpHandler(ashx) page which would take a querystring and set that as the imageUrl property of the image control

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

Now in DisplayImage.ashx, you can override the Processrequest like 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);

    } 

Web.config changes:-

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

Details here and here.

Hope this helps..

ydobonmai
I think you got the question wrong. I have to show the image in the image control of asp.net not just on the web-page.
HotTester
@HotTester, the above code/concept would display the image in the IMG control. To explain more, suppose you have a page (EmployeeDetails.aspx) where you have the image control, the imageUrl of which is "DisplayImage.ashx?employeeId=12". When you request for "EmployeeDetails.aspx" page and when the image control is getting rendered, a request will go to the DisplayImage.ashx (an HttpHandler) which is just going to output the binary data for the image and then that would get displayed in the Image control of the Employee.aspx. Let me know If I still misunderstood your question.
ydobonmai