Click here to Skip to main content
15,888,216 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: (untagged)
In my web application, I need to display binary image from database in image web control.
I can convert binary data to image, but how to bind it to Image web control?

Please help me.
Posted

1 solution

Hi andiyuniar
I've had this issue before. The way I handled it was to "spoof" a content page to change it's ContentType to display images. I would pass it an id via query string and then set the imageurl to the spoofed page.

Here's an example that should help you. Please keep in mind, I don't know if it's the best solution, but it works for me. Also, there could be some performance hits if you have too many images being loaded on your page via this method.

Create a WebForm... i called mine LoadImage.aspx
Place this code in the Load event

<br />if (Request.QueryString["Id"] != null)<br />        {<br />            int Id = Convert.ToInt32(Request.QueryString["Id"].ToString());<br />            byte[] image = null;<br />            //You will have to add your connection information and grab the image from the database<br />            // ToDo: Load binary image into image variable;<br /><br />                if (image!=null)<br />                {<br />                    MemoryStream ms = new MemoryStream(image);<br />                    Bitmap b = new Bitmap(Image.FromStream(ms));<br /><br />                    Response.ContentType = "image/jpeg";<br />                    b.Save(Response.OutputStream, ImageFormat.Jpeg);<br />                }<br />                else<br />                {<br />                    Bitmap b = new Bitmap(Image.FromFile(Server.MapPath("images/noimage.png")));<br /><br />                    Response.ContentType = "image/jpeg";<br />                    b.Save(Response.OutputStream, ImageFormat.Jpeg);<br />                }<br />        }<br />        else<br />        {<br />            Bitmap b = new Bitmap(Image.FromFile(Server.MapPath("images/noimage.png")));<br /><br />            Response.ContentType = "image/jpeg";<br />            b.Save(Response.OutputStream, ImageFormat.Jpeg);<br />        }<br />


Then on the page you want to bind your image to just set the ImageUrl property to the webform plus query string.

<br />  Image1.ImageUrl = "LoadImage.aspx?Id=1";<br />


Happy Coding

 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900