有人知道如何使用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; 
有帮助吗?

解决方案

我不会从bitmapimage开始,作为您加载数据的方式。实际上,我根本不会使用它。相反,我会使用 BitmapDecoder::Create 并传递 BitmapCreateOptions.PreservePixelFormat. 。那么您可以访问 BitmapFrame 您感兴趣并检查它 Format 现在应该产生CMYK的财产。

然后,如果您确实需要显示图像,则可以分配 BitmapFrame, ,这也是 BitmapSource 子类, Image::Source.

其他提示

我的测试结果与您的测试结果有所不同。

  • Windows 7的:
    • ImageFlags:ColorsPacergb
    • PixelFormat:Pixelformat32Bppcmyk(8207)
  • Windows Server 2008 R2:
    • ImageFlags:ColorsPacergb
    • PixelFormat:Pixelformat32Bppcmyk(8207)
  • Windows Server 2008:
    • ImageFlags:colorspaceycck
    • PixelFormat:格式24BPPRGB

以下代码应起作用:

    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;
    }

我遇到了相同的问题,如果您使用的.NET 2.0,那么BitMapDecoder将无法工作。.您要做的就是阅读文件,然后简单检查以查看字节说该文件是什么。 如何使用C#在ASP.NET中识别CMYK图像 希望这对某人有帮助。

欢呼 - 杰里米

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top