如何在.NET 2.0 中打开多帧 TIFF 图像格式图像?
-
03-07-2019 - |
题
Image.FromFile(@"path\filename.tif")
或者
Image.FromStream(memoryStream)
即使源是多帧 TIFF 文件,两者都会生成仅具有一帧的图像对象。 如何加载保留这些帧的图像文件? 使用 Image.SaveAdd 方法逐帧保存 tiff。它们在其他查看器中工作,但 .NET Image 方法不会加载这些帧,只会加载第一个帧。
这是否意味着无法从我传入用作帧的位图集合的方法返回多帧 TIFF?
解决方案
这是我使用的:
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;
}
其他提示
我能够使用以下方法处理多帧 tiff。
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();
我没有尝试过 .net 2.0 中的解决方案,但 MSDN 显示类成员存在。它确实解决了我在 .net 4.5.2 中的问题。
位图.Dispose();
For 循环之后,您需要处理位图。否则,当尝试在其他进程中使用同一文件时,您会收到错误“文件使用其他进程”。
不隶属于 StackOverflow