Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call an aspx page and have it return an image?

Tags:

c#

asp.net

image

******Editing post to 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 that page:

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

newpage.aspx has the following code, based on Jason's sample 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 are not supported error"

I think its close, though.

Also, for anyone just reading this, this solution is needed to get around the fact that skype does not have an https option for its skype buttons which tell the status of the skype user. Creating this proxy page will allow this to work without causing a 'mixed' security alert in the browser.

like image 743
ericgr Avatar asked Oct 06 '10 14:10

ericgr


People also ask

How do I add an image to ASPX?

To upload the image, click on Choose File and then browse to the image which you want to upload. Once the image is selected then the name of the image will be displayed next to the Choose File button as shown in the following screenshot. As you can see the that images.

What is ASPX HTML?

HTML Page : A Web page with user interface and content static information that does not go through any server-side processing. ASPX Page : A Web page with user interface and dynamic content information that is coming from server-side. This type of page will render the HTML content in the browser.

How are ASPX files generated?

An ASPX file is an Active Server Page Extended (ASPX) file, a webpage generated by web servers running the Microsoft ASP.NET framework. It contains one or more scripts written in VBScript or C# code that the web server processes into HTML, which is sent to the user's web browser.


2 Answers

So, here was the final working code: thanks everyone for your help tracking this down bit by 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>
like image 160
ericgr Avatar answered Nov 14 '22 23:11

ericgr


Just to expand on Aliostad's answer, here's a snippet that might help you:

protected void Page_Load(object sender, EventArgs e)
{
    Response.ContentType = "image/jpeg";

    var data = // .... get the content of the images as bytes. e.g. 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 did use something similar recently on a project to return images from an aspx page. Unfortunately, I don't have that code in front of me now.

like image 28
Jason Evans Avatar answered Nov 14 '22 22:11

Jason Evans