title: Using an HTTP handler in ASP.NET to generate an image for display in email
body: <p>I'm generating a barcode image as the response from an HTTP handler, like so: </p>
  
  <pre><code>public void ProcessRequest(HttpContext context)
  {
      context.Response.Clear();
      context.Response.ContentType = "image/Jpeg";
      MemoryStream ms = new MemoryStream();
      Bitmap objBitmap = GenerateBarcode(context.Request.Params["Code"]);
      objBitmap.Save(ms, ImageFormat.Jpeg);
      context.Response.BinaryWrite(ms.GetBuffer());
  }
  </code></pre>
  
  <p>I go to <a href="http://www.MyWebsite.com/MyProject/BarCode.aspx?code=12345678" rel="nofollow noreferrer">http://www.MyWebsite.com/MyProject/BarCode.aspx?code=12345678</a> and it works great.  Likewise I stick <code>&lt;img alt="" src="http://www.MyWebsite.com/MyProject/BarCode.aspx?code=12345678"&gt;</code> on my webpage and it works great.  But I stick that same image tag in an HTML email and it doesn't show up (at least not in MS Outlook 2007; I haven't tested other email clients yet.)</p>
  
  <p>I'm guessing this is somehow related to the fact that I'm using an HTTP handler, as other, static images in the email are showing up fine.  How can I fix this so that the image shows up?  (I can't just use a static image because the code is determined at the time the email is sent.)</p>
  
  <p><strong>Update:</strong></p>
  
  <p>It turns out I hadn't noticed a key detail.  The image isn't just not showing up; rather the image src attribute is getting replaced with "<a href="http://portal.mxlogic.com/images/transparent.gif" rel="nofollow noreferrer">http://portal.mxlogic.com/images/transparent.gif</a>".  I've determined that using the .aspx or .ashx extensions triggers this replacement (or probably any extension except those expected for images like .gif or .jpg), and that including a query string in the URL <em>also</em> triggers this, even when it's a standard image extension.  I guess it's some overzealous security feature.  So including an image like BarCode.aspx?code=12345678 just isn't going to work.</p>
  
  <p>It occurs to me that I could do something like <code>&lt;img alt="" src="http://www.MyWebsite.com/MyProject/12345678/BarCode.jpg"&gt;</code> and then create a handler for all files named BarCode.jpg.  Here 12345678/ isn't an actual path but it wouldn't matter since I'm redirecting the request to a handler, and I could scrape the code value from that phony path in the URL.  But, I'd probably have to change some IIS setting to have requests for .jpg files handled by ASP.NET, and I'd want to still make sure that <em>other</em> JPEGs besides my BarCode.jpg loaded normally.</p>
  
  <p>Honestly, I'm not sure if it's worth the hassle.</p>