Solved

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

Posted on 2011-02-24
6
754 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
  • 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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