Solved

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

Posted on 2011-02-24
6
741 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now