logo


ASP.NET Write Image to Response.OutputStream

Printer Friendly Version


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; } }
}


Submission Date:  9/23/2005 2:57:34 PM
Submitted By:  Robbe Morris
My Home Page:  http://www.robbemorris.com

My Biography
Robbe has been a Microsoft MVP for C# since 2004. He is also the co-founder of EggHeadCafe. Robbe has extensive experience with web technologies, .NET, C#, CTI based applications, system administration, .NET Compact Framework, and data modeling. In his spare time, he blogs from time to time at http://robbemorris.blogspot.com