如何使用C#识别CMYK图像 [英] How to identify CMYK images using C#
问题描述
有人知道如何使用C#正确识别CMYK图像吗?我找到了使用ImageMagick的方法,但是我需要一个.NET解决方案。我在网上发现了3个代码段,其中只有一个在Windows 7中有效,但是在Windows Server 2008 SP2中都失败了。我至少在Windows Server 2008 SP2中需要它。这是我发现的东西:
Does anybody know how to properly identify CMYK images using C#? I found how to do it using ImageMagick, but I need a .NET solution. I found 3 code snippets online, only one works in Windows 7, but all fail in Windows Server 2008 SP2. I need it to work at least in Windows Server 2008 SP2. Here is what I've found:
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。
I wouldn't start with BitmapImage as your way of loading the data. In fact, I wouldn't use it at all for this. Instead I would use BitmapDecoder::Create
and pass in BitmapCreateOptions.PreservePixelFormat
. Then you can access the BitmapFrame
you're interested in and check its Format
property which should now yield CMYK.
然后,如果您确实需要显示图像,则只需分配 BitmapFrame
,它也是一个 BitmapSource
子类,将其转换为 Image :: Source
。
Then, if you really need to display the image, you can just assign the BitmapFrame
, which is also a BitmapSource
subclass, to an Image::Source
.
这篇关于如何使用C#识别CMYK图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!