How to show a image in database in the image control of Asp.net?

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..

Leave a Comment