Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to identify CMYK images in ASP.NET using C#
    primarykey
    data
    text
    <p>Does anybody know how to properly identify CMYK images in ASP.NET using C#? When I check the <code>Flags</code> attribute of a <code>Bitmap</code> instance, I get incorrect results.</p> <p>I have created three images to test this: cmyk.jpg, rgb.jpg and gray.jpg. These are respectively CMYK, RGB and Grayscale images.</p> <p>This is my test code:</p> <pre><code>static void Main(string[] args) { Bitmap bmpCMYK = new Bitmap("cmyk.jpg"); Bitmap bmpRGB = new Bitmap("rgb.jpg"); Bitmap bmpGray = new Bitmap("gray.jpg"); Console.WriteLine("\t\tRgb\tCmyk\tGray\tYcbcr\tYcck\tPixelFormat"); Console.WriteLine("cmyk.jpg\t{0}\t{1}\t{2}\t{3}\t{4}\t{5}", IsSet(bmpCMYK, System.Drawing.Imaging.ImageFlags.ColorSpaceRgb), IsSet(bmpCMYK, System.Drawing.Imaging.ImageFlags.ColorSpaceCmyk), IsSet(bmpCMYK, System.Drawing.Imaging.ImageFlags.ColorSpaceGray), IsSet(bmpCMYK, System.Drawing.Imaging.ImageFlags.ColorSpaceYcbcr), IsSet(bmpCMYK, System.Drawing.Imaging.ImageFlags.ColorSpaceYcck), bmpCMYK.PixelFormat); Console.WriteLine("rgb.jpg\t\t{0}\t{1}\t{2}\t{3}\t{4}\t{5}", IsSet(bmpRGB, System.Drawing.Imaging.ImageFlags.ColorSpaceRgb), IsSet(bmpRGB, System.Drawing.Imaging.ImageFlags.ColorSpaceCmyk), IsSet(bmpRGB, System.Drawing.Imaging.ImageFlags.ColorSpaceGray), IsSet(bmpRGB, System.Drawing.Imaging.ImageFlags.ColorSpaceYcbcr), IsSet(bmpRGB, System.Drawing.Imaging.ImageFlags.ColorSpaceYcck), bmpRGB.PixelFormat); Console.WriteLine("gray.jpg\t{0}\t{1}\t{2}\t{3}\t{4}\t{5}", IsSet(bmpGray, System.Drawing.Imaging.ImageFlags.ColorSpaceRgb), IsSet(bmpGray, System.Drawing.Imaging.ImageFlags.ColorSpaceCmyk), IsSet(bmpGray, System.Drawing.Imaging.ImageFlags.ColorSpaceGray), IsSet(bmpGray, System.Drawing.Imaging.ImageFlags.ColorSpaceYcbcr), IsSet(bmpGray, System.Drawing.Imaging.ImageFlags.ColorSpaceYcck), bmpGray.PixelFormat); bmpCMYK.Dispose(); bmpRGB.Dispose(); bmpGray.Dispose(); Console.ReadLine(); } private static bool IsSet(Bitmap bitmap, System.Drawing.Imaging.ImageFlags flag) { return (bitmap.Flags &amp; (int)flag) == (int)flag; } </code></pre> <p>This produces the following output: <img src="https://i.stack.imgur.com/fdBL5.gif" alt="Test results"></p> <p>I have checked the actual images and cmyk.jpg really <em>is</em> a CMYK image.</p> <p>Apparently, this is a "known issue". Alex Gil had the same problem in WPF (see this question: <a href="https://stackoverflow.com/questions/4315335/how-to-identify-cmyk-images-using-c">How to identify CMYK images using C#</a>) and he managed to solve it by using a <code>BitmapDecoder</code> class to load the images. I'm a bit uncomfortable using that solution in ASP.NET because it requires me to add references to WindowsBase.dll and PresentationCore.dll and I'm not sure I want those in a web project.</p> <p>Does anyone know of any other pure .NET solutions to check if an image is in the CMYK format that I can safely use in ASP.NET?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload