Extract frame from multiple tiff pages - C #

Have a multi-page tiff, and I want to extract the page [n] / frame [n] from this Tiff file and save it.

If my multi-page tiff has 3 frames, after I extract one page / frame, I want to leave

1 image with 2 pages / frames and

1 image has only 1 page / frame.

+4
source share
2 answers

Below is the code to save the last frame in a multi-frame tiff in a single-page tiff file. (To use this code, you need to add a link to PresentationCore.dll).

Stream imageStreamSource = new FileStream(imageFilename, FileMode.Open, FileAccess.Read, FileShare.Read); MemoryStream memstream = new MemoryStream(); memstream.SetLength(imageStreamSource.Length); imageStreamSource.Read(memstream.GetBuffer(), 0, (int)imageStreamSource.Length); imageStreamSource.Close(); BitmapDecoder decoder = TiffBitmapDecoder.Create(memstream,BitmapCreateOptions.PreservePixelFormat,BitmapCacheOption.Default); Int32 frameCount = decoder.Frames.Count; BitmapFrame imageFrame = decoder.Frames[0]; MemoryStream output = new MemoryStream(); TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Frames.Add(imageFrame); encoder.Save(output); FileStream outStream = File.OpenWrite("Image.Tiff"); output.WriteTo(outStream); outStream.Flush(); outStream.Close(); output.Flush(); output.Close(); 
+6
source
  public void SaveFrame(string path, int frameIndex, string toPath) { using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) { BitmapDecoder dec = BitmapDecoder.Create(stream, BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.None); BitmapEncoder enc = BitmapEncoder.Create(dec.CodecInfo.ContainerFormat); enc.Frames.Add(dec.Frames[frameIndex]); using (FileStream tmpStream = new FileStream(toPath, FileMode.Create)) { enc.Save(tmpStream); } } } 
+1
source

All Articles