开发者

How can I call an aspx page and have it return an 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.


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>


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.


To expand further (on Jason's answer), you will not want to read the response stream into a StreamReader (as the result is not text). What you can do use the aspx page as the src of your image on the page that needs it. For example:

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

Because MyDynamicImage.aspx returns an image as its response, it can be treated as an image (as if you were pointing to a static .jpg, e.g.).


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

            Response.ContentType = "Image/jpg";


You can use the code posted above in the ProcessRequest method of a class that implements IHttpHandler. This are a bit more lightweight than doing it with a Page as you will avoid the page lifecycle.

There is an example at http://wiki.asp.net/page.aspx/687/http-handlers-to-handle-images/. It's a bit more involved than what you need but you should be able to modify it to your needs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜