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:
有人知道如何使用c#正确识别myck图像吗?我发现了如何使用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;
3 个解决方案
#1
5
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.
我不会以BitmapImage作为你加载数据的方式。事实上,我根本就不会用它。相反,我将使用BitmapDecoder::创建并传递BitmapCreateOptions.PreservePixelFormat。然后,您可以访问感兴趣的位图框架,并检查它的Format属性,现在应该会产生CMYK。
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
.
然后,如果您确实需要显示图像,您可以将位mapframe(也是位mapsource子类)分配给图像::Source。
#2
2
My test results are a bit different than yours.
我的测试结果和你的有点不同。
- Windows 7:
- ImageFlags: ColorSpaceRgb
- ImageFlags:ColorSpaceRgb
- PixelFormat: PixelFormat32bppCMYK (8207)
- PixelFormat:PixelFormat32bppCMYK(8207)
- windows7: ImageFlags: ColorSpaceRgb PixelFormat: PixelFormat32bppCMYK (8207)
- Windows Server 2008 R2:
- ImageFlags: ColorSpaceRgb
- ImageFlags:ColorSpaceRgb
- PixelFormat: PixelFormat32bppCMYK (8207)
- PixelFormat:PixelFormat32bppCMYK(8207)
- Windows Server 2008 R2: ImageFlags: ColorSpaceRgb PixelFormat: PixelFormat32bppCMYK (8207)
- Windows Server 2008:
- ImageFlags: ColorSpaceYcck
- ImageFlags:ColorSpaceYcck
- PixelFormat: Format24bppRgb
- PixelFormat:Format24bppRgb
- Windows服务器2008:ImageFlags: ColorSpaceYcck PixelFormat: Format24bppRgb
The following code should work:
下列守则应有效:
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;
}
#3
0
I ran into the same issues and if your using .net 2.0 then BitmapDecoder will not work.. what you want to do is read the file and just plain check to see that what the bytes say the file is.. How to identify CMYK images in ASP.NET using C# Hope this helps someone.
我遇到了同样的问题,如果你使用。net 2.0, BitmapDecoder就不会起作用。你要做的就是读取文件并进行简单的检查,看看字节表示的文件是什么。如何在ASP中识别CMYK图像。NET使用c#希望这能帮助某人。
Cheers - Jeremy
欢呼——杰里米
#1
5
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.
我不会以BitmapImage作为你加载数据的方式。事实上,我根本就不会用它。相反,我将使用BitmapDecoder::创建并传递BitmapCreateOptions.PreservePixelFormat。然后,您可以访问感兴趣的位图框架,并检查它的Format属性,现在应该会产生CMYK。
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
.
然后,如果您确实需要显示图像,您可以将位mapframe(也是位mapsource子类)分配给图像::Source。
#2
2
My test results are a bit different than yours.
我的测试结果和你的有点不同。
- Windows 7:
- ImageFlags: ColorSpaceRgb
- ImageFlags:ColorSpaceRgb
- PixelFormat: PixelFormat32bppCMYK (8207)
- PixelFormat:PixelFormat32bppCMYK(8207)
- windows7: ImageFlags: ColorSpaceRgb PixelFormat: PixelFormat32bppCMYK (8207)
- Windows Server 2008 R2:
- ImageFlags: ColorSpaceRgb
- ImageFlags:ColorSpaceRgb
- PixelFormat: PixelFormat32bppCMYK (8207)
- PixelFormat:PixelFormat32bppCMYK(8207)
- Windows Server 2008 R2: ImageFlags: ColorSpaceRgb PixelFormat: PixelFormat32bppCMYK (8207)
- Windows Server 2008:
- ImageFlags: ColorSpaceYcck
- ImageFlags:ColorSpaceYcck
- PixelFormat: Format24bppRgb
- PixelFormat:Format24bppRgb
- Windows服务器2008:ImageFlags: ColorSpaceYcck PixelFormat: Format24bppRgb
The following code should work:
下列守则应有效:
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;
}
#3
0
I ran into the same issues and if your using .net 2.0 then BitmapDecoder will not work.. what you want to do is read the file and just plain check to see that what the bytes say the file is.. How to identify CMYK images in ASP.NET using C# Hope this helps someone.
我遇到了同样的问题,如果你使用。net 2.0, BitmapDecoder就不会起作用。你要做的就是读取文件并进行简单的检查,看看字节表示的文件是什么。如何在ASP中识别CMYK图像。NET使用c#希望这能帮助某人。
Cheers - Jeremy
欢呼——杰里米