Solved

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

Posted on 2011-02-24
6
765 Views
Last Modified: 2012-06-22
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.
0
Comment
Question by:nkc1112
[X]
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
6 Comments
 
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.
0
 

Author Comment

by:nkc1112
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.
0
 
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.
0
Technology Partners: 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

by:
Todd Gerbert earned 500 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());

			context.Response.BinaryWrite(buffer.ToArray());
		}
	}

	public bool IsReusable
	{
		get
		{
			return false;
		}
	}
}

Open in new window

0
 

Author Comment

by:nkc1112
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.
0
 
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.
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 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