2014-01-27 56 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