2008-09-04 75 views
0

我試圖將多頁顏色tiff文件轉換爲C#中的CompressionCCITT3 tiff。我意識到我需要確保所有像素都是1位。我還沒有找到這個在線的有用的例子。壓縮一個TIF文件

回答

0

我看到上面的代碼,它看起來像是用手動邏輯轉換每個像素。

這會適合你嗎?

進口System.Drawing.Imaging

'得到的顏色TIF文件

昏暗bmpColorTIF作爲新的位圖( 「C:\ color.tif」)

' 選擇的的一個區域TIF(將抓住所有幀)

昏暗rectColorTIF作爲新矩形(0,0,bmpColorTIF.Width,bmpColorTIF.Height)

「克隆矩形作爲1位山口或TIF

昏暗bmpBlackWhiteTIF爲位圖= bmpColorTIF.Clone(rectColorTIF,PixelFormat.Format1bppIndexed)

「你想用新的位圖(保存等)

什麼...

注意:有大量的像素格式可供選擇。

1

Pimping聲明:我爲製作.NET成像軟件的公司Atalasoft工作。

使用dotImage,這個任務就變得像這樣:

FileSystemImageSource source = new FileSystemImageSource("path-to-your-file.tif", true); // true = loop over all frames 
// tiff encoder will auto-select an appropriate compression - CCITT4 for 1 bit. 
TiffEncoder encoder = new TiffEncoder(); 
encoder.Append = true; 

// DynamicThresholdCommand is very good for documents. For pictures, use DitherCommand 
DynamicThresholdCommand threshold = new DynamicThresholdCommand(); 

using (FileStream outstm = new FileStream("path-to-output.tif", FileMode.Create)) { 
    while (source.HasMoreImages()) { 
     AtalaImage image = source.AcquireNext(); 
     AtalaImage finalImage = image; 
     // convert when needed. 
     if (image.PixelFormat != PixelFormat.Pixel1bppIndexed) { 
      finalImage = threshold.Apply().Image; 
     } 
     encoder.Save(outstm, finalImage, null); 
     if (finalImage != image) { 
      finalImage.Dispose(); 
     } 
     source.Release(image); 
    } 
} 

鮑勃·鮑威爾的例子是很好的,只要它去,但它有一些問題,而不是其中最重要的是,它是使用一個簡單的閾值,如果你想要速度並且實際上並不關心你的輸出是什麼樣的,或者你的輸入域是真的非常黑白的話,那麼這是非常棒的 - 只是用顏色表示。二值化是一個棘手的問題。當您的任務是在24/24時減少可用信息時,如何保留正確的信息並丟棄其他信息是一項挑戰。 DotImage有六種不同的工具(IIRC)用於二值化。從我的角度來看,SimpleThreshold是桶的底部。

1

我建議在深入編碼之前首先使用tiff和圖像工具來試驗預期的結果。我發現VIPS是一個方便的工具。下一個選擇是研究LibTIFF可以做什麼。我使用c#免費使用LibTiff.NET獲得了很好的結果(另請參閱stackoverflow)。我對GDI tiff功能非常失望,雖然你的milage可能會有所不同(我需要缺失的16位灰度)。 也可以使用LibTiff實用程序(即,請參閱http://www.libtiff.org/man/tiffcp.1.html

相關問題