Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 827
mirezus Avatar asked Dec 30 '08 21:12

mirezus


People also ask

What is a multi page TIFF file?

A multipage TIFF file is a single TIF file which contains multiple tif images. MTIFF files are a bit like PDF in that they contain multiple pages, but the similarity ends there.

How do I create a multipage TIFF?

Open the JPEG image and click File->Print from the application menu. Choose TIFF Image Printer 11.0 from the list of printers and then click the Print button. Enter the same filename, choose TIFF Multipaged (*. tif) and check Append to file, then click Save.

Can TIFF store multiple images?

Advantages of TIFF files. The file can work as a container for smaller-sized JPEGs, storing multiple images in one master raster graphic.

How do I split an image in a TIF file?

❓ How can I split TIFF document? First, you need to add a file for split: drag & drop your TIFF file or click inside the white area for choose a file. Then click the 'Split' button.


2 Answers

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;
}
like image 162
Otávio Décio Avatar answered Sep 29 '22 04:09

Otávio Décio


I was able to handle the multi-frame tiff by using the below method.

Image multiImage = Image.FromFile(sourceFile);

multiImage.Save(destinationFile, tiff, prams);

int pageCount = multiImage.GetFrameCount(FrameDimension.Page);

for (int page = 1; page < pageCount; page++ )
{
    multiImage.SelectActiveFrame(FrameDimension.Page,page);
    multiImage.SaveAdd(dupImage,prams);
}

multiImage.SaveAdd(newPage, prams);
multiImage.Dispose(); 

I have not tried the solution in .net 2.0 but MSDN shows the class members to exist. It did fix my problem in .net 4.5.2.

like image 32
Chris Avatar answered Sep 29 '22 04:09

Chris