BreakHead
BreakHead

Reputation: 10672

How to convert system.drawing.image to system.web.ui.webcontrols.image

I use to store image in bytes and able to convert it to system.drawing.image but not sure how to render it on page

Thanks

Upvotes: 5

Views: 21440

Answers (2)

TheVillageIdiot
TheVillageIdiot

Reputation: 40507

System.Drawing.Image represents image or picture that you can render, print, save to file, resize, create thumbnail from etc. But System.Web.UI.WebControls.Image is a web control that you can use to show images on web pages.

To show dynamically created image on webpage you need some handler or other mechanism that sends image to calling page.

Here is an article on 4guysfromrolla that explains this concept.

Here is another one on developerfusion.com in C#

I found this very detailed article on MSDN by Scott Mitchel.

Upvotes: 3

Pavel Morshenyuk
Pavel Morshenyuk

Reputation: 11471

You can create ASPX page that will return image file as byte array with appropriate headers information, to get image you will be able to call this page like imagemanager.aspx?imgid=31337

Then in your main page in system.web.ui.webcontrols.image control set ImageUrl property to your script path:

ctrlImage.ImageUrl = "imagemanager.aspx?imgid=31337";

Here is example of method to output you image in imagemanager.aspx:

    private void TransmitBytes(byte[] bytes, string outFileName)
    {
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + outFileName);
        Response.AddHeader("Content-Length", bytes.Length.ToString());
        Response.ContentType = "image/jpeg";
        Response.BinaryWrite(bytes);
        Response.End();
    }

Upvotes: 5

Related Questions