Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


System.Drawing.Image type need to be displayed in Image control of

Posted on 2011-02-24
Medium Priority
Last Modified: 2012-06-22

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 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.
Question by:nkc1112
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34970186
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.

Author Comment

ID: 34970215
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.
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34970260
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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 33

Accepted Solution

Todd Gerbert earned 2000 total points
ID: 34970351
So your ASPX markup might look something like:
<asp:Image ID="ImageControl" runat="server" ImageUrl="~/ImageProvider.ashx" />

Open in new window

And the code for ImageProvider.ashx would look like (I didn't include code to generate the image as I understand you already have that):
<%@ WebHandler Language="C#" Class="ImageProvider" %>

using System;
using System.Web;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

public class ImageProvider : IHttpHandler

	public void ProcessRequest(HttpContext context)
		Image image = GenerateImage();

		using (MemoryStream buffer = new MemoryStream())
			image.Save(buffer, ImageFormat.Jpeg);

			context.Response.ContentType = "image/jpeg";
			context.Response.AddHeader("Content-Length", buffer.Length.ToString());


	public bool IsReusable
			return false;

Open in new window


Author Comment

ID: 34970544
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.
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34970636
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.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question