2014-01-27 55 views
1

我想了解并实现Tiff压缩的一段代码。 (第一种方法体积庞大)和图像保存方法http://msdn.microsoft.com/en-us/library/ytz20d80%28v=vs.110%29.aspx(第二种方法仅适用于Windows 7计算机,但不适用于Windows 2003或2008服务器)我已经使用了2种独立技术 - 使用第三方dll的LibTiff.NEt(第一种方法很笨重)和图像保存方法,http://msdn.microsoft.com/en-us/library/ytz20d80%28v=vs.110%29.aspxc#Tiff文件压缩方法

现在我正在寻找探索这第三种方法。

using System.Windows.Forms; 
using System.Windows.Media.Imaging; 
using System.Drawing.Imaging; 

     int width = 800; 
     int height = 1000; 
     int stride = width/8; 
     byte[] pixels = new byte[height*stride]; 

     // Try creating a new image with a custom palette. 
     List<System.Windows.Media.Color> colors = new List<System.Windows.Media.Color>(); 
     colors.Add(System.Windows.Media.Colors.Red); 
     colors.Add(System.Windows.Media.Colors.Blue); 
     colors.Add(System.Windows.Media.Colors.Green); 
     BitmapPalette myPalette = new BitmapPalette(colors); 

     // Creates a new empty image with the pre-defined palette 

     BitmapSource image = BitmapSource.Create(
      width, 
      height, 
      96, 
      96, 
      System.Windows.Media.PixelFormats.BlackWhite, 
      myPalette, 
      pixels, 
      stride); 


FileStream stream = new FileStream(Original_File, FileMode.Create); 
TiffBitmapEncoder encoder = new TiffBitmapEncoder(); 
encoder.Compression = TiffCompressOption.Ccitt4; 
encoder.Frames.Add(BitmapFrame.Create(image)); 
encoder.Save(stream); 

但我没有对这里发生的事情有充分的了解。

显然有一种类型的压缩技术正在应用的内存流。但我有点困惑如何将这个应用到我的具体情况。我有一个原始的tiff文件,我想使用此方法将其压缩设置为CCITT并将其保存。谁能帮忙?

我复制了上面的代码并运行代码。但我的最终输出文件是纯黑色背景图像。虽然积极的一面是正确的压缩类型。

http://msdn.microsoft.com/en-us/library/ms616002%28v=vs.110%29.aspx

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.tiffcompressoption%28v=vs.100%29.aspx

http://social.msdn.microsoft.com/Forums/vstudio/en-US/1585c562-f7a9-4cfd-9674-6855ffaa8653/parameter-is-not-valid-for-compressionccitt4-on-windows-server-2003-and-2008?forum=netfxbcl

+0

你为什么添加asp.net标签?你在做这个是一个asp.net项目吗? – mbeckish

+0

是的。我有一个网页,允许用户输入,然后转换成一个tiff文件。这个tiff文件的原始压缩类型是LZW。我需要CCITT压缩格式。 – Philo

+0

您可能会遇到问题,因为[ASP.NET服务中不支持图形命名空间](http://stackoverflow.com/q/390532/21727) – mbeckish

回答

0

LibTiff.net有点笨重,因为它是基于关闭的libtiff,有其自身的一系列问题。

我的公司(Atalasoft)有能力很容易地做到这一点,并且free version of the SDK将执行您想要的任务,只需要一些限制。对于重新编码文件中的代码应该是这样的:你应该知道的

public bool ReencodeFile(string path) 
{ 
    AtalaImage image = new AtalaImage(path); 
    if (image.PixelFormat == PixelFormat.Pixel1bppIndexed) 
    { 
     TiffEncoder encoder = new TiffEncoder(); 
     encoder.Compression = TiffCompression.Group4FaxEncoding; 
     image.Save(path, encoder, null); // destroys the original - use carefully 
     return true; 
    } 
    return false; 
} 

事情:

  • 这个代码将只在1bpp映射图像
  • 此代码将无法正常工作在多页TIFF上正常工作
  • 此代码确实不是在原始文件中保留元数据

我想要代码至少检查一下。如果你倾向于有更好的保留什么的文件的内容的解决方案,你会想这样做:

public bool ReencodeFile(string origPath, string outputPath) 
{ 
    if (origPath == outputPath) throw new ArgumentException("outputPath needs to be different from input path."); 
    TiffDocument doc = new TiffDocuemnt(origPath); 

    bool needsReencoding = false; 
    for (int i=0; i < doc.Pages; i++) { 
     if (doc.Pages[i].PixelFormat == PixelFormat.Pixel1bppIndexed) { 
      doc.Pages[i] = new TiffPage(new AtalaImage(origPath, i, null), TiffCompression.Group4FaxEncoding); 
      needsReencoding = true; 
     } 
    } 
    if (needsReendcoding) 
     doc.Save(outputPath); 
    return needsReencoding; 
} 

该解决方案将尊重文档元数据文件中的所有页面以及。

+0

谢谢。我仍然试图用上述方法来解决我的问题。如果你能帮上忙。请告诉我。 – Philo

+0

另外,如果图像本来不是1bpp? – Philo

+0

在CCITT中,您不能编码除1bpp之外的任何内容。对于其他位深度,您必须选择不同的编解码器(通常是flate或jpeg)。我建议你花一点时间阅读图像压缩和编解码器。另外,如果你看看TIFF规范并且认为“我可以完全自己写这个格式” - 再想一想,这是微妙而粗糙的(一种不寻常的组合)。 – plinth