Monday, March 7, 2011

How to open a multi-frame TIFF imageformat image in .NET 2.0?

Image.FromFile(@"path\filename.tif")

or

Image.FromStream(memoryStream)

both produce image objects with only one frame even though the source is a multi-frame TIFF file. How do you load an image file that retains these frames? The tiffs are saved using the Image.SaveAdd methods frame by frame. They work in other viewers but .NET Image methods will not load these frames, only the first.

Does this mean that there is no way to return a multi-frame TIFF from a method where I am passing in a collection of bitmaps to be used as frames?

From stackoverflow
  • Here's what I use:

    private List<Image> GetAllPages(string file)
            {
                List<Image> images = new List<Image>();
                Bitmap bitmap = (Bitmap)Image.FromFile(file);
                int count = bitmap.GetFrameCount(FrameDimension.Page);
                for (int idx = 0; idx < count; idx++)
                {
                    // save each frame to a bytestream
                    bitmap.SelectActiveFrame(FrameDimension.Page, idx);
                    MemoryStream byteStream = new MemoryStream();
                    bitmap.Save(byteStream, ImageFormat.Tiff);
    
                    // and then create a new Image from it
                    images.Add(Image.FromStream(byteStream));
                }
                return images;
            }
    
    mirezus : Does this mean that there is no way to return a multi-frame TIFF from a method where I am passing in a collection of bitmaps to be used as frames?
    Otávio Décio : Pretty much yes - you have to remember that in a multiframe TIFF each frame can have its own set of encoding options (compression, color, quality). Bitmaps on the other hand are much simpler animals.
  • Take a look at the DoOpen method on this page:

    http://www.bobpowell.net/addframes.htm

0 comments:

Post a Comment