Solved

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

Posted on 2011-02-24
6
769 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
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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Suggested Courses

734 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