Determining a TIFF's compression

Last post 10-28-2009 9:54 PM by Song-Tian - MSFT. 2 replies.

Sort Posts:

  • Determining a TIFF's compression

    10-22-2009, 5:22 PM

    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.

    When you ask a question, remember to click "mark as answered" when you get a reply which answers your question.


    My latest ASP.NET AJAX blog entries.
  • Re: Determining a TIFF's compression

    10-28-2009, 5:20 PM

    Bump. 

    When you ask a question, remember to click "mark as answered" when you get a reply which answers your question.


    My latest ASP.NET AJAX blog entries.
  • Re: Determining a TIFF's compression

    10-28-2009, 9:54 PM
    Answer

    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.

    Roy Tian.
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (3 items)