You need to set the content type to "image/jpg" or relevant image MIME.
Response.ContentType = "Image/jpg";
You need to set the content type to "image/jpg" or relevant image MIME.
Response.ContentType = "Image/jpg";
Hi there.
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.
Cheers. Jas.
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 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.
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>