2010-11-30 77 views
19

有誰知道如何使用C#正確識別CMYK圖像嗎?我發現如何使用ImageMagick來完成它,但我需要一個.NET解決方案。我在網上發現了3個代碼片段,只有一個在Windows 7中可用,但在Windows Server 2008 SP2中都失敗。我需要它至少在Windows Server 2008 SP2中工作。這是我發現的:如何使用C#識別CMYK圖像


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

    bool isCmyk; 

    // WPF 
    BitmapImage wpfImage = new BitmapImage(new Uri(imgFile)); 

    // false in Win7 & WinServer08, wpfImage.Format = Bgr32 
    isCmyk = (wpfImage.Format == PixelFormats.Cmyk32); 

    // Using GDI+ 
    Image img = Image.FromFile(file); 

    // false in Win7 & WinServer08 
    isCmyk = ((((ImageFlags)img.Flags) & ImageFlags.ColorSpaceCmyk) == 
     ImageFlags.ColorSpaceCmyk); 

    // true in Win7, false in WinServer08 (img.PixelFormat = Format24bppRgb) 
    isCmyk = ((int)img.PixelFormat) == 8207; 
+0

都是你的測試盒的x86或64? – 2010-11-30 16:22:17

+0

兩者都是64位機器。它可能是GDI + dll嗎? – 2010-11-30 17:22:30

回答

5

我不會以BitmapImage作爲加載數據的方式來啓動。事實上,我完全不會使用它。相反,我會使用BitmapDecoder::Create並通過BitmapCreateOptions.PreservePixelFormat。然後,您可以訪問您感興趣的BitmapFrame並檢查其現在應產生CMYK的Format屬性。

然後,如果您確實需要顯示圖像,則可以將BitmapFrame(也是BitmapSource子類)指定給Image::Source

0

我遇到了同樣的問題,如果你使用.net 2.0,那麼BitmapDecoder將無法正常工作..你想要做的是讀取文件,只是簡單地檢查,看看字節說文件是什麼.. How to identify CMYK images in ASP.NET using C#希望這可以幫助某人。

乾杯 - 傑里米

2

我的測試結果是比你有點不同。

  • 的Windows 7:
    • ImageFlags:ColorSpaceRgb
    • 的PixelFormat:PixelFormat32bppCMYK(8207)
  • 的Windows Server 2008 R2:
    • ImageFlags:ColorSpaceRgb
    • 的PixelFormat:PixelFormat32bppCMYK( 8207)
  • 的Windows Server 2008:
    • ImageFlags:ColorSpaceYcck
    • 的PixelFormat:Format24bppRgb

下面的代碼應該工作:

public static bool IsCmyk(this Image image) 
    { 
     var flags = (ImageFlags)image.Flags; 
     if (flags.HasFlag(ImageFlags.ColorSpaceCmyk) || flags.HasFlag(ImageFlags.ColorSpaceYcck)) 
     { 
      return true; 
     } 

     const int PixelFormat32bppCMYK = (15 | (32 << 8)); 
     return (int)image.PixelFormat == PixelFormat32bppCMYK; 
    }