Reputation: 202
i'm trying to display an image from the database in an ASP.NET web page. I'm using a generic handler, and it works fine on firefox, chrome and IE9, but not in IE8. That's my generic handler code:
public void ProcessRequest(HttpContext context)
{
byte[] FileContent = null;
if (context.Request.QueryString["imagen"] != null)
{
FileContent = GetImageFromDatabase(context.Request.QueryString["imagen"]);
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(FileContent);
}
}
And i have an image in my asp page markup:
<asp:Image ID="imgInicio" runat="server" Width="100%" AlternateText="Inicio" />
Finally i call this on the load event;
imgInicio.ImageUrl = String.Format(@"~/ShowImage.ashx?imagen={0}", idImage);
I have tried it all. Any help would be appreciated.
Upvotes: 2
Views: 3648
Reputation: 1
I had the same problem with my Internet Explorer 8 image of the database not working but all other browsers do.
I used BinaryWrite. I'm falling on a page of a performance test between BinaryWrite and OutputStream.write.
OutputStream.write is more performance and whether its use .. gonna set all your problem. Its set my problems on my side.
url: http://www.dotnetperls.com/response-binarywrite
Upvotes: 0
Reputation: 11
I had problem with images showing up broken in ie8 and not in any other browsers and it turned out that my .jpg images were accidently saved as CMYK instead of RGB. Chrome, Safari, Firefox, ie9 had no problems, it was just ie8 that showed them as broken.
Upvotes: 1
Reputation: 202
Problem solved
I found the solution. I don´t know why in Internet Explorer 8 you have to specify the format of the image. I had to write code to format it using System.Drawing namespace. I added a helper class with the following methods:
public static Image ImageFromBytes(byte[] buffer)
{
try
{
using (MemoryStream ms = new MemoryStream(buffer))
{
return Image.FromStream(ms);
}
}
catch
{
return null;
}
}
public static byte[] FormatImage(byte[] buffer, ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
using (Image img = ImageFromBytes(buffer))
{
img.Save(ms, format);
}
return ms.ToArray();
}
}
And then i call the mehod inside the handler:
context.Response.BinaryWrite(ImageHelper.FormatImage(FileContent, System.Drawing.Imaging.ImageFormat.Png));
I think that, when we convert the bytes to a System.Drawing.Image and then use the Save method to retrieve the bytes again, the framework does something, maybe add or remove some bytes, that other browsers ignore, but they are important to Internet Explorer 8. That's what i think, but it's not certain. The thing is that it works for me
Upvotes: 0
Reputation: 15253
Start by running the query in SQL Server Management Studio - or some other means that definitely confirms that there is data coming back.
Check that the data is in fact in the database? Are you using an Image field in the DB?
There is nothing to suggest from the question that this is IE-related. If the above steps confirm the existence and return of the data, continue by following devio's steps.
UPDATE
Per feedback it certainly looks like an IE8 (or earlier)/PNG issue.
Upvotes: -1
Reputation: 37215
You have several possibilities to debug this situation:
Set a breakpoint in ProcessRequest to find out whether IE requests the images from the server
Use the Developer Tools (F12) to find out whether the images are loaded in the page, but are not rendered
Use the Developer Tools to change the compatibility and doctype settings (were they available in IE8?) and see whether these changes have any effect on image rendering
Make sure the images are really stored and served as .png when the web server claims they are .png (some browser may be more susceptible to MIME types than others)
In a browser that renders the images, download the images to your local harddisk and use an image viewer to confirm that the image format is really png
Verify that the problem is not related to earlier IE's alpha-channel and transparency problems
Upvotes: 1