【问题标题】:How to identify CMYK images using C#如何使用 C# 识别 CMYK 图像
【发布时间】:2023-03-19 01:34:01
【问题描述】:

有人知道如何使用 C# 正确识别 CMYK 图像吗?我找到了使用 ImageMagick 的方法,但我需要一个 .NET 解决方案。我在网上找到了 3 个代码 sn-ps,只有一个在 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; 

【问题讨论】:

  • 你的两个测试盒都是 x86 还是 x64?
  • 两者都是 64 位机器。会不会是 GDI+ dll?
  • 两种操作系统的img.PixelFormat 返回什么? wpfImage.Format 怎么样?
  • 啊... GDI+。 .NET 既完全依赖又完全害怕的库。 System.Drawing 对 GDI+ 的依赖比 .NET 框架中的其他任何东西都更怪异、“内存不足”错误和莫名其妙的异常......
  • Gabe,我修改了代码 sn-p 以显示 wpfImage 和 img.PixelFormat 返回的内容

标签: .net wpf gdi+ cmyk


【解决方案1】:

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

  • Windows 7:
    • ImageFlags:ColorSpaceRgb
    • 像素格式:PixelFormat32bppCMYK (8207)
  • Windows Server 2008 R2:
    • ImageFlags:ColorSpaceRgb
    • 像素格式:PixelFormat32bppCMYK (8207)
  • Windows 服务器 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;
    }

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,如果您使用 .net 2.0,那么 BitmapDecoder 将无法正常工作..您要做的是读取文件并简单检查文件中的字节内容.. @987654321 @希望这对某人有所帮助。

    干杯 - 杰里米

    【讨论】:

      【解决方案3】:

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

      然后,如果您确实需要显示图像,您可以将BitmapFrame(也是BitmapSource 的子类)分配给Image::Source

      【讨论】:

        猜你喜欢
        • 2011-07-01
        • 1970-01-01
        • 2020-05-15
        • 2013-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-01
        • 1970-01-01
        相关资源
        最近更新 更多