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开发者_Python百科.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;
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.
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.
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
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.
精彩评论