locked
Determining a TIFF's compression RRS feed

  • Question

  • User-1684099974 posted

    I am looking for a way to open a TIFF and determine what compression it was saved in (LZW, JPEG, none, etc.).  Thank you for your time.

    Thursday, October 22, 2009 5:22 PM

Answers

  • User78336405 posted

    Hi,

    There has a method to open tiff file with HttpHandler:

    using System;
    using System.Web;
    using System.IO;
    using System.Drawing;
    using System.Drawing.Imaging;
    
    namespace MyHandler
    {
        /// <summary>
        /// Summary description for NewHandler.
        /// </summary>
        public class NewHandler : IHttpHandler
        {
            int Width = 0;
            int Height = 0; 
            public void ProcessRequest(HttpContext context)
                {
                    if (context.Request.Params["height"] != null)
                    {
                        try
                        {
                            Height = int.Parse(context.Request.Params["height"]);
                        }
                        catch
                        {
                            Height = 0;
                        }
                    }
                    if (context.Request.Params["width"] != null)
                    {
                        try
                        {
                            Width = int.Parse(context.Request.Params["width"]);
                        }
                        catch
                        {
                            Width = 0;
                        }
                    }
                    if (Width <= 0 && Height <= 0)
                    {
                        context.Response.Clear();
                        context.Response.ContentType = getContentType(context.Request.PhysicalPath); context.Response.WriteFile(context.Request.PhysicalPath);
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Clear();
                        context.Response.ContentType = getContentType(context.Request.PhysicalPath);
                        byte[] buffer = getResizedImage(context.Request.PhysicalPath, Width, Height);
                        context.Response.OutputStream.Write(buffer, 0, buffer.Length);
                        context.Response.End();
                    }
                }
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
            byte[] getResizedImage(String path, int width, int height)
            {
                Bitmap imgIn = new Bitmap(path);
                double y = imgIn.Height;
                double x = imgIn.Width;
                double factor = 1;
                if (width > 0)
                {
                    factor = width / x;
                }
                else if (height > 0)
                {
                    factor = height / y;
                }
                System.IO.MemoryStream outStream = new System.IO.MemoryStream();
                Bitmap imgOut = new Bitmap((int)(x * factor), (int)(y * factor));
                Graphics g = Graphics.FromImage(imgOut);
                g.Clear(Color.White);
                g.DrawImage(imgIn, new Rectangle(0, 0, (int)(factor * x), (int)(factor * y)), new Rectangle(0, 0, (int)x, (int)y), GraphicsUnit.Pixel);
                imgOut.Save(outStream, getImageFormat(path));
                return outStream.ToArray();
            }
            string getContentType(String path)
            {
                switch (Path.GetExtension(path))
                {
                    case ".bmp": return "Image/bmp";
                    case ".gif": return "Image/gif";
                    case ".jpg": return "Image/jpeg";
                    case ".png": return "Image/png";
                    case ".tif": return "Image/tiff";    
                    default: break;
                }
                return "";
            }
            ImageFormat getImageFormat(String path)
            {
                switch (Path.GetExtension(path))
                {
                    case ".bmp": return ImageFormat.Bmp;
                    case ".gif": return ImageFormat.Gif;
                    case ".jpg": return ImageFormat.Jpeg;
                    case ".png": return ImageFormat.Png;
                    case ".tif": return ImageFormat.Tiff;
                    default: break;
                }
                return ImageFormat.Jpeg;               
             }
        }
    }


     


     

    <httpHandlers>
    <add verb="*" path="*.tif" type="MyHandler.NewHandler"/>
    <add verb="*" path="*.bmp" type="MyHandler.NewHandler"/>
    <add verb="*" path="*.jpg" type="MyHandler.NewHandler"/>
    <add verb="*" path="*.gif" type="MyHandler.NewHandler"/>
    <add verb="*" path="*.png" type="MyHandler.NewHandler"/>	
    </httpHandlers>


     

     If there have been any misunderstanding, please let me know.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 28, 2009 9:54 PM

All replies

  • User-1684099974 posted

    Bump. 

    Wednesday, October 28, 2009 5:20 PM
  • User78336405 posted

    Hi,

    There has a method to open tiff file with HttpHandler:

    using System;
    using System.Web;
    using System.IO;
    using System.Drawing;
    using System.Drawing.Imaging;
    
    namespace MyHandler
    {
        /// <summary>
        /// Summary description for NewHandler.
        /// </summary>
        public class NewHandler : IHttpHandler
        {
            int Width = 0;
            int Height = 0; 
            public void ProcessRequest(HttpContext context)
                {
                    if (context.Request.Params["height"] != null)
                    {
                        try
                        {
                            Height = int.Parse(context.Request.Params["height"]);
                        }
                        catch
                        {
                            Height = 0;
                        }
                    }
                    if (context.Request.Params["width"] != null)
                    {
                        try
                        {
                            Width = int.Parse(context.Request.Params["width"]);
                        }
                        catch
                        {
                            Width = 0;
                        }
                    }
                    if (Width <= 0 && Height <= 0)
                    {
                        context.Response.Clear();
                        context.Response.ContentType = getContentType(context.Request.PhysicalPath); context.Response.WriteFile(context.Request.PhysicalPath);
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Clear();
                        context.Response.ContentType = getContentType(context.Request.PhysicalPath);
                        byte[] buffer = getResizedImage(context.Request.PhysicalPath, Width, Height);
                        context.Response.OutputStream.Write(buffer, 0, buffer.Length);
                        context.Response.End();
                    }
                }
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
            byte[] getResizedImage(String path, int width, int height)
            {
                Bitmap imgIn = new Bitmap(path);
                double y = imgIn.Height;
                double x = imgIn.Width;
                double factor = 1;
                if (width > 0)
                {
                    factor = width / x;
                }
                else if (height > 0)
                {
                    factor = height / y;
                }
                System.IO.MemoryStream outStream = new System.IO.MemoryStream();
                Bitmap imgOut = new Bitmap((int)(x * factor), (int)(y * factor));
                Graphics g = Graphics.FromImage(imgOut);
                g.Clear(Color.White);
                g.DrawImage(imgIn, new Rectangle(0, 0, (int)(factor * x), (int)(factor * y)), new Rectangle(0, 0, (int)x, (int)y), GraphicsUnit.Pixel);
                imgOut.Save(outStream, getImageFormat(path));
                return outStream.ToArray();
            }
            string getContentType(String path)
            {
                switch (Path.GetExtension(path))
                {
                    case ".bmp": return "Image/bmp";
                    case ".gif": return "Image/gif";
                    case ".jpg": return "Image/jpeg";
                    case ".png": return "Image/png";
                    case ".tif": return "Image/tiff";    
                    default: break;
                }
                return "";
            }
            ImageFormat getImageFormat(String path)
            {
                switch (Path.GetExtension(path))
                {
                    case ".bmp": return ImageFormat.Bmp;
                    case ".gif": return ImageFormat.Gif;
                    case ".jpg": return ImageFormat.Jpeg;
                    case ".png": return ImageFormat.Png;
                    case ".tif": return ImageFormat.Tiff;
                    default: break;
                }
                return ImageFormat.Jpeg;               
             }
        }
    }


     


     

    <httpHandlers>
    <add verb="*" path="*.tif" type="MyHandler.NewHandler"/>
    <add verb="*" path="*.bmp" type="MyHandler.NewHandler"/>
    <add verb="*" path="*.jpg" type="MyHandler.NewHandler"/>
    <add verb="*" path="*.gif" type="MyHandler.NewHandler"/>
    <add verb="*" path="*.png" type="MyHandler.NewHandler"/>	
    </httpHandlers>


     

     If there have been any misunderstanding, please let me know.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 28, 2009 9:54 PM