2008-12-30 41 views
9
Image.FromFile(@"path\filename.tif") 

如何在.NET 2.0中打開多幀TIFF圖像格式圖像?

Image.FromStream(memoryStream) 

都產生圖像對象用即使源是一個多幀TIFF文件僅一個幀。 如何加載保留這些幀的圖像文件?使用Image.SaveAdd方法逐幀保存tiff。他們在其他查看器中工作,但.NET Image方法不會加載這些幀,只有第一個。

這是否意味着我沒有辦法從一個方法中返回一個多幀TIFF,在這個方法中我傳入一組要用作幀的位圖?

回答

22

這是我使用:

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; 
} 
+0

這是否意味着我沒有辦法從一個方法中返回一個多幀TIFF,在這個方法中,我傳入一組要用作幀的位圖集合? – mirezus 2008-12-30 21:38:02

0

bitmap.Dispose();

For循環後,您需要處理位圖。否則,當您嘗試在其他進程中使用相同的文件時,您會收到錯誤「使用其他進程的文件」。

1

我能夠通過使用下面的方法來處理多幀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中的問題。