ASP.NET Write Image to Response.OutputStream

Class for reading an image from disk and writing it out to the Response.Outputstream. This example implements the IHttpHandler interface used in custom http handlers.

Class for reading an image from disk and writing it out to the Response.Outputstream. This example implements the IHttpHandler interface used in custom http handlers.


Class for reading an image from disk and writing it out to the Response.Outputstream. This example implements the IHttpHandler interface used in custom http handlers. If you need help with setting this up, click here:
http://www.eggheadcafe.com/articles/20030113.asp

This is also a nice solution for tracking when a recipient of an HTML formatted
email actually opened and read it. Just insert your desired database code and
it can be triggered just before writing the image. You can learn alot about
your member base for other marketing endeavors...

Please take a moment to rate this Tip (opens in new window):

http://www.turnkeytools.com/polls/default.asp?POLLID=10000067&DOMAINID=4610


using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;

public class Writer : IHttpHandler
{
public void ProcessRequest(HttpContext oContext)
{

string sFileName="";
string sPath=oContext.Server.MapPath(".");
try
{
sFileName = oContext.Request["name"].ToString().Trim();
if (sFileName.Length <1) { return; }
Image oImg = Image.FromFile(sPath + @"\images\" + sFileName,true);
oImg.Save(oContext.Response.OutputStream,ImageFormat.Jpeg);
oImg.Dispose();
}
catch (Exception e) { oContext.Response.Write(e.Message); }

}

public bool IsReusable { get { return true; } }
}

By Robbe Morris   Popularity  (4592 Views)
Picture
Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of NullSkull.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.  Robbe also loves to scuba dive and go deep sea fishing in the Florida Keys or off the coast of Daytona Beach. Microsoft MVP