如何使用C#识别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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的测试结果和你的有点不同。
以下代码应该有效:
My test results are a bit different than yours.
The following code should work:
我不会从 BitmapImage 作为加载数据的方式开始。事实上,我根本不会为此使用它。相反,我会使用
BitmapDecoder::Create
并传入BitmapCreateOptions.PreservePixelFormat
。然后,您可以访问您感兴趣的
BitmapFrame
并检查其Format
属性现在应该生成 CMYK。然后,如果您确实需要显示图像,只需将 BitmapFrame(也是 BitmapSource 子类)分配给 Image::Source 即可代码>.
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 inBitmapCreateOptions.PreservePixelFormat
. Then you can access theBitmapFrame
you're interested in and check itsFormat
property which should now yield CMYK.Then, if you really need to display the image, you can just assign the
BitmapFrame
, which is also aBitmapSource
subclass, to anImage::Source
.我遇到了同样的问题,如果您使用 .net 2.0 那么 BitmapDecoder 将无法工作..您想要做的就是读取文件,然后简单地检查一下字节所说的文件是什么.. 如何使用 C# 识别 ASP.NET 中的 CMYK 图像 希望这对某人有帮助。
干杯 - 杰里米
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.
Cheers - Jeremy