2008-09-04 123 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