How can I call an aspx page and return the image?

****** Editing a post for a newer code sample based on comments *******

So, to be clear, I have two files. The first file is called FinalImage.aspx, and here is the code for this page:

<html> <body> <img src="newpage.aspx" /> </body> </html> 

newpage.aspx has the following code based on Jason's example in the comments below:

 <%@ Page Language="C#" %> <script runat="server" language="c#"> protected void Page_Load(object sender, EventArgs e) { Response.ContentType = "image/png"; byte[] data = System.IO.File.ReadAllBytes("http://mystatus.skype.com/smallclassic/eric-greenberg"); Response.OutputStream.Write(data, 0, data.Length); Response.OutputStream.Flush(); Response.End(); } </script> 

If I call FinalImage.aspx, I see a broken image.

If I call newpage.aspx directly, I get a "URI Formats unsupported error"

I think this is close.

In addition, for those who simply read this, this solution is necessary to get around the fact that skype does not have the https option for its skype buttons, which report the status of the skype user. Creating this proxy page will allow you to work without causing a "mixed" security warning in your browser.

+6
c # image
source share
5 answers

So, here is the final working code: thanks to everyone for your help, tracking it a bit (so to speak ...)

 <%@ Page Language="C#" %> <script runat="server" language="c#"> protected void Page_Load(object sender, EventArgs e) { Response.ContentType = "image/png"; System.Net.WebClient wc = new System.Net.WebClient(); byte[] data = wc.DownloadData("http://mystatus.skype.com/smallclassic/eric-greenberg"); Response.OutputStream.Write(data, 0, data.Length); Response.OutputStream.Flush(); Response.End(); } </script> 
+8
source share

Just to expand Aliostad's answer, here is a snippet that may help you:

 protected void Page_Load(object sender, EventArgs e) { Response.ContentType = "image/jpeg"; var data = // .... get the content of the images as bytes. eg File.ReadAllBytes("path to image"); Response.OutputStream.Write(data, 0, data.Length); Response.OutputStream.Flush(); // Not sure if needed, but doesn't hurt to have it. Response.End(); } 

Not sure if the above is 100% correct, but I recently used something like this in a project to return images from an aspx page. Unfortunately, I do not have this code in front of me.

+3
source share

To continue further (according to Jason's answers), you will not want to read the response stream in StreamReader (as a result, this is not text). You can use the aspx page as src of your image on the desired page. For example:

 <html> <image src="~/MyDynamicImage.aspx"/> </html> 

Since MyDynamicImage.aspx returns the image as an answer, it can be considered as an image (as if you were pointing to a static .jpg, for example).

+1
source share

You need to set the content type to "image / jpg" or the corresponding MIME image.

  Response.ContentType = "Image/jpg"; 
0
source share

You can use the code specified in the ProcessRequest method of the class that implements IHttpHandler. This is a little easier than doing it with a page, as you will avoid the page life cycle.

Example: http://wiki.asp.net/page.aspx/687/http-handlers-to-handle-images/ . This is a bit more involved than what you need, but you should be able to change it to suit your needs.

0
source share

All Articles