yigolden / TiffLibrary

C# library for decoding and encoding Tag Image File Format (TIFF) files.
MIT License
58 stars 17 forks source link

ImageSharp.Image frames count is always 1 #11

Closed sergiuciudin closed 2 years ago

sergiuciudin commented 2 years ago

Trying to get frames from a tiff with multiple images but count is always 1 so i can get only first image.

using MemoryStream inputStream = new MemoryStream(bytes); using var tiff = Image.Load(inputStream); tiff.Frames.Count // returns 1

yigolden commented 2 years ago

Duplicate of #3

I don't think this is possible with the Image.Frames API. Because in ImageSharp, frames in an image must have the same size. They are exclusively used in GIF format. While in multipage TIFF, pages can have different sizes. They should be treated like a collection of images instead of frames.

However, you can use the following code to enumerate the pages and process them using ImageSharp API.

using TiffFileReader reader = await TiffFileReader.OpenAsync(inputStream, leaveOpen: true);
TiffStreamOffset ifdOffset = reader.FirstImageFileDirectoryOffset;
int index = 0;
while (!ifdOffset.IsZero)
{
    TiffImageFileDirectory ifd = await reader.ReadImageFileDirectoryAsync(ifdOffset);
    TiffImageDecoder decoder = await reader.CreateImageDecoderAsync(ifd);
    Image<Rgba32> image = new Image<Rgba32>(decoder.Width, decoder.Height);
    await decoder.DecodeAsync(image);
    await image.SaveAsPngAsync(@$"C:\Test\{++index}.png");
    ifdOffset = ifd.NextOffset;
}