从多页面提取帧tiff - c#
问题描述:
有多页面tiff,我想从这个Tiff文件中提取页面[n]/frame [n]并保存。从多页面提取帧tiff - c#
如果我多页TIFF有3架,之后我提取一个页/帧 - 我想留下具有2页/帧的并且具有
1图像
1图像只有1页/帧。
答
下面是一些代码,用于将多帧tiff中的最后一帧保存为单页tiff文件。 (为了使用此代码,您需要添加对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();
答
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);
}
}
}
为了使用此代码,你需要添加一个引用您也可以在“[程序文件] /引用程序”文件夹中找到的PresentationCore.dll中。此程序集包含'System.Windows.Media.Imaging'命名空间。 – Crackerjack 2011-07-13 22:27:06