1

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;
2
  • Yes, please show us some code, as your question can go many different ways and we want to give you specific assistance. Jun 8, 2010 at 17:59
  • Code added as requested!
    – jehuty
    Jun 8, 2010 at 18:20

4 Answers 4

1

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.

The easiest way to fix this is to check to see if the image exists in the Http-handler (in the image.ashx file.) before returning.

  if(image == null) {image = new blankImage();}

If it's not there, substitute it with a blank image. That way this is no image not found icon. If you really want it to disappear and not hold the image size, just make the blank image a 1x1 square.

1
  • We have two problems here, though. The programming one is that image never evaluates to null during the Page Load event. Given the page life cycle, image.ashx?id=XXX will only try to be fetched after Page Load has long executed. Just for completeness: The 2nd problem (requirements) is that the table holding the image has captions and title bars that must go as well, since there'll be no content!
    – jehuty
    Jun 8, 2010 at 18:26
0

Can you not just use a NullReferenceException, or am I misunderstanding the question?

try
{ 
    //try to get the photo
}
catch (NullReferenceException)
{
    //handle the error
}

You could also check if image == null I suppose, that may make more sense in your situation.

4
  • This may work, however if possible, you want to avoid using try catch and exceptions to control program flow Jun 8, 2010 at 17:59
  • No, I don't think you misunderstood - that's just it. But then I would be using an exception to control the flow of the application, would't I? Since the image not being there is expected in some situations!
    – jehuty
    Jun 8, 2010 at 18:04
  • @Lerxst Yeah, my thoughts exactly!
    – jehuty
    Jun 8, 2010 at 18:05
  • Yeah, I suppose I was just being lazy :P I've added an addition. Jun 8, 2010 at 18:07
0

since the ashx gets executed after page_load, you can make it return a 1x1 square, however if you want to entirely hide the column, you are going to have some issues because of the lifecycle.

you could create a placeholder on your page, and build your table dynamically. If you can avoid using an ashx, and instead do the image retrieval and rendering in your code-behind, you will be able to know when to hide the column or not

0

Even though this makes the page fetch the image twice, I'm using small images and on very few pages, so I think it is worth the cost.

This is the code I added to the page:

public static bool CheckImageExistance(string url)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 
                request.Method = "HEAD";       

                request.Credentials = CredentialCache.DefaultCredentials;

                HttpWebResponse response = request.GetResponse() as HttpWebResponse; 
                response.Close();
                return (response.StatusCode == HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                return false;
            }

And it works as expected. Thanks for all the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.