C# 如何在 .NET 2.0 中打开多帧 TIFF 图像格式图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/401561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open a multi-frame TIFF imageformat image in .NET 2.0?
提问by mirezus
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.
尽管源是多帧 TIFF 文件,但两者都生成只有一帧的图像对象。如何加载保留这些帧的图像文件?使用 Image.SaveAdd 方法逐帧保存 tiff。它们在其他查看器中工作,但 .NET Image 方法不会加载这些帧,只会加载第一个。
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?
这是否意味着无法从我传入要用作帧的位图集合的方法中返回多帧 TIFF?
采纳答案by Otávio Décio
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;
}
回答by Adem Canpolat
bitmap.Dispose();
bitmap.Dispose();
After For loop you need to Dispose bitmap. Otherwise you get error "file using other process" when try to use same file in other process.
在 For 循环之后,您需要处理位图。否则,当尝试在其他进程中使用相同文件时,您会收到错误“文件使用其他进程”。
回答by Chris
I was able to handle the multi-frame tiff by using the below method.
我能够使用以下方法处理多帧 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();
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.
我还没有尝试过 .net 2.0 中的解决方案,但 MSDN 显示类成员存在。它确实解决了我在 .net 4.5.2 中的问题。