如何使用C#识别CMYK图像

Ale*_*Gil 20 .net wpf gdi+ cmyk

有人知道如何使用C#正确识别CMYK图像吗?我发现如何使用ImageMagick,但我需要一个.NET解决方案.我在网上找到了3个代码段,只有一个在Windows 7中运行,但在Windows Server 2008 SP2中都失败了.我需要它至少在Windows Server 2008 SP2中工作.这是我发现的:


    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; 
Run Code Online (Sandbox Code Playgroud)

Dre*_*rsh 5

我不会以BitmapImage作为加载数据的方式开始.事实上,我根本不会使用它.相反,我会使用BitmapDecoder::Create并传入BitmapCreateOptions.PreservePixelFormat.然后你可以访问BitmapFrame你感兴趣的并检查它的Format属性,现在应该产生CMYK.

然后,如果你真的需要显示图像,你可以只分配它BitmapFrame,也是一个BitmapSource子类Image::Source.


Sha*_*ser 5

我的测试结果和你的有点不同。

  • Windows 7的:
    • ImageFlags:ColorSpaceRgb
    • 像素格式:PixelFormat32bppCMYK (8207)
  • Windows Server 2008 R2:
    • ImageFlags:ColorSpaceRgb
    • 像素格式:PixelFormat32bppCMYK (8207)
  • 视窗服务器 2008:
    • ImageFlags:ColorSpaceYcck
    • 像素格式: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;
    }
Run Code Online (Sandbox Code Playgroud)