title: How to handle null return from custom HttpHandler in asp.net?
body: <p>I'm using a custom ashx HttpHandler to retrieve gif images from a database and show it on a website - when the image exists, it works great. </p>
  
  <p>However, there are cases when the image will not exist, and I'd like to have the html table holding the image to become invisible so the "image not found" icon is not shown.</p>
  
  <p>But since the HttpHandler is not synchronous, all my attempts checking for image size at Page_Load were frustrated. Any ideas on how this can be accomplished?</p>
  
  <p>EDIT::</p>
  
  <p>Here's how it's happening so far:</p>
  
  <p>This is my handler:</p>
  
  <pre><code> public void ProcessRequest(HttpContext context)
          {
              using (Image image = GetImage(context.Request.QueryString["id"]))
              {
                  context.Response.ContentType = "image/gif";
                  image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
              }
          }
  
          private Image GetImage(string id)
          {
              try
              {
                  System.IO.MemoryStream ms;
                  byte[] rawImage;
                  Image finalImage;
                  // Database specific code!      
  rawImage = getImageFromDataBase(id);
  
                  ms = new System.IO.MemoryStream(rawImage, 0, rawImage.Length);
                  ms.Write(rawImage, 0, rawImage.Length); 
  
                  finalImage = System.Drawing.Image.FromStream(ms, true);
  
                  return finalImage;
              }
              catch (Exception ex)
              {
                  System.Console.WriteLine("ERROR:::: " + ex.Message);
                  return null;
              }
          }
  </code></pre>
  
  <p>And I use it like this:</p>
  
  <pre><code>myImage.ImageUrl = "Image.ashx?id=" + properId;
  </code></pre>