2010-12-23 66 views
6

當我旋轉圖像時,.Net切換tiff編碼。有沒有辦法讓我保持CCITT傳真4(組4傳真編碼),而不是將它切換到LZW?這是我如何旋轉磁盤上的圖像。.Net Image.Save將CTTIT傳真4中的tiff更改爲LZW

System.Drawing.Image img = System.Drawing.Image.FromFile(input); 
//rotate the picture by 90 degrees 
img.RotateFlip(RotateFlipType.Rotate90FlipNone); 
img.Save(input, System.Drawing.Imaging.ImageFormat.Tiff); 

感謝, 布賴恩

更新:這是基於鏈接下面的文章中的代碼。我想在這裏添加完整的代碼。此外,我設置水平分辨率,因爲位圖默認爲96 DPI。

//create an object that we can use to examine an image file 
System.Drawing.Image img = System.Drawing.Image.FromFile(input); 

//rotate the picture by 90 degrees 
img.RotateFlip(RotateFlipType.Rotate90FlipNone); 

// load into a bitmap to save with proper compression 
Bitmap myBitmap = new Bitmap(img); 
myBitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution); 

// get the tiff codec info 
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/tiff"); 

// Create an Encoder object based on the GUID for the Compression parameter category 
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Compression; 

// create encode parameters 
EncoderParameters myEncoderParameters = new EncoderParameters(1); 
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, (long)EncoderValue.CompressionCCITT4); 
myEncoderParameters.Param[0] = myEncoderParameter; 

// save as a tiff 
myBitmap.Save(input, myImageCodecInfo, myEncoderParameters); 

// get encoder info for specified mime type 
private static ImageCodecInfo GetEncoderInfo(String mimeType) 
{ 
    int j; 
    ImageCodecInfo[] encoders; 
    encoders = ImageCodecInfo.GetImageEncoders(); 
    for (j = 0; j < encoders.Length; ++j) 
    { 
     if (encoders[j].MimeType == mimeType) 
      return encoders[j]; 
    } 
    return null; 
} 

回答

6

Image類不會給你必要的粒度控制。

爲此,您需要讀入位圖,創建TIFF編碼器,設置壓縮類型的參數,然後讓Bitmap對象使用該編解碼器和參數保存圖像。

這裏是一個應該引起你的正確方向邁出的例子:

http://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoder.compression.aspx

我沒有VS打開,我的Mac上的時刻。

以下是詳細信息:

http://social.msdn.microsoft.com/Forums/en/windowswic/thread/e05f4bc2-1f5c-4a10-bd73-86a676dec554

+0

感謝的作品。它將DPI從300改爲96。有人知道如何設置它嗎? – BrianK 2010-12-23 17:22:55