Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net

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

like image 778
BreakHead Avatar asked Aug 09 '10 06:08

BreakHead


2 Answers

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();
    }
like image 86
Pavel Morshenyuk Avatar answered Nov 13 '22 22:11

Pavel Morshenyuk


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.

like image 3
TheVillageIdiot Avatar answered Nov 13 '22 22:11

TheVillageIdiot