Click here to Skip to main content
15,905,912 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi there,

Can anyone tell me how to get the returned bitmap image to an Image server control.
My code is below. How can I return it to an Image server control?


The problem I encountered is, When displaying the returned Bitmap Image directly to the web page all other Html as well as Server controls become invisble.Only bitmap image is visible.I want to avoid that.So I supposed to display the bitmap in an Image server control...


Graphics obj_Graphics;
  Bitmap obj_Bitmap = new Bitmap(500, 500);
  obj_Graphics = Graphics.FromImage(obj_Bitmap);

Code for Drawing Image as:

System.Web.HttpContext.Current.Response.ContentType = "image/jpeg";
        obj_Bitmap.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);
        obj_Bitmap.Dispose();
        obj_Graphics.Dispose();
        System.Web.HttpContext.Current.Response.End();

        return obj_Bitmap;


Thanks in advance
Posted
Updated 16-May-11 18:36pm
v3

You need to separate rendering of your page and rendering of the image into two "pages". You cannot do both of them in the same operation.

Here is what is happening:

The user's browser requests your web page using HTTP GET e.g. GET http://mabel.ca/
The server's job is to reply to this request with the HTML document created by your web form.

What you are doing above is replying with the image instead of the html.
What you need to do is reply with valid HTML and that HTML should include a valid img tag to another page that you'll create e.g. <img src="http://mabel.ca/title.aspx" alt="title image"/>

Remember a web page is only a single HTML document. For every image, stylesheet, Flash animation, etc the client's browser must make a separate GET request.

The job of this new page is ONLY to render the image. The browser will know it's an image (a) because the img tag tells it to expect an image; (b) because you've set the HTTP the Content-Type header; (c) because if all else fails, browsers are pretty good at guessing content types.

So:
1. Make a page that just renders your image.
2. Make a page for your content. It should include an img tag that links to #1.
 
Share this answer
 
Just to add, your second page, will ideally take an id on the URL to look up the image bytes, then use Response.WriteBytes or Response.WriteFile, depending on if it's a file on the file system, to pass that information back. You also need to set the response header to say it's an image.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900