Link to home
Start Free TrialLog in
Avatar of nkc1112
nkc1112

asked on

System.Drawing.Image type need to be displayed in Image control of asp.net(C#)

Hello,

I have method that generates images if we pass a string, the return type of method is image of type system.drawing.image and when i use that image and trying to assign to image control of asp.net i.e. system.web.ui.webcontrols the image is not displayed and i get cannot covert of type drawing.image to system.web.ui.webcontrol image.
I looked up in online but not much very helpful, i just need to display the system.drawing.image in a web page.
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

I believe you will need to save the image to a disk file, if only temporary, and set the ImageUrl property of your Image control to point to that file.
Avatar of nkc1112
nkc1112

ASKER

Actually saving image is not possible as per our requirement.  I need to some other way to do it.
I have been searching for past one day nothing was helpful but sure i can try saving an image, could you please provide some sample code as reference like how to save an image to folder in run time? and then how to access it.
Well, you can't just throw a bunch of bytes that comprise an image into the middle of an HTML page, the client's browser needs to see an <img src="http://someurl.jpg"></img> tag, and in turn the browser will download the image file pointed to by the src attribute and display it on screen.

What you can do, however, is make the ImageUrl of your Image control point to an ASHX page, and have that .ashx file output the bytes of the image.
ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nkc1112

ASKER

Thanks for your sample code, to be more clear..  

my code,

system.drawing.image = CovertToImage(string xyz);

//CovertToImage is some method that converts string into an image of type system.drawing.image... now i have a image control on web page i have to catch the image and need to assign to image control on web page.

Above code you provided i was unable to get it exactly were to use as i never used an http handler previously.
Right-click your project, add new item, I believe it's called a "Generic Handler"

It creates a .ashx file, and when a web browser asks for this file the code-behind is run and the only thing sent to the client is what you explicitly send with Response.Write et al - there is not markup or HTML for these files.