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.
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.
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?
EDIT::
Here's how it's happening so far:
This is my handler:
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;
}
}
And I use it like this:
myImage.ImageUrl = "Image.ashx?id=" + properId;