Development – Leet 4 Life

Software Development Code Samples and More

Skip to: Content | Sidebar | Footer

Displaying TIFF Images in ASP.NET

3 October, 2012 | ASP.NET, C#. NET | By: Michael Adams

I needed to display TIFF images that were stored in the database. Unfortunately it is not as simple as just setting the MIME Type and writing out the binary. The reason for this is because the TIFF image type is only supported in IE currently. So I asked Google and the overwhelming response I saw was to convert the TIFF into another image type and then write out the binary to the response stream. Here is my take on the solution:

Written in C# (the image is sent via a Generic Handler hence the “context.Response”):

using (MemoryStream stream = new MemoryStream(Content))
   System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
   img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
   context.Response.ContentType = "image/gif";

It should be noted that there are some drawbacks to this simple approach. The first major drawback is that this approach balloons the file size, you can also end up running out of memory with large files. Also, another drawback is the fact that some TFF properties don’t properly convert over to other formats and will cause runtime exceptions. In these cases you will need to remove the properties from the TIFF files prior to conversion.

Original Code posted Here

Write a comment