Reputation: 6255
How can i compare pixel formats of 2 images?
i have tried this:
if (img1.PixelFormat > img2.PixelFormat)
but "Format8bppIndexed" got rated as being bigger than "Format24bppRgb"
what am i doing wrong?
Upvotes: 0
Views: 1098
Reputation: 5629
This is an old question, but since it seems no one has actually posted the obvious answer on it... Image.GetPixelFormatSize(pixelformat)
returns the amount of used bits per pixel for the given pixel format. So for Format16bppArgb1555
it would return 16
.
Int32 pixSize1 = Image.GetPixelFormatSize(img1.PixelFormat);
Int32 pixSize2 = Image.GetPixelFormatSize(img2.PixelFormat);
if (pixSize1 > pixSize2)
{
...
}
Though I really wonder why you would ever check that, unless it is to differentiate between indexed formats, where conversion to lower BPP could mean that the higher palette indices you have become impossible to write.
Upvotes: 0
Reputation: 1075
If you want to compare the images by their bit depths, try the following, this worked for me:
/// <summary>
/// Returns the bit depth of <paramref name="image"/>.
/// </summary>
public static int GetBitDepth(this Image image)
{
return ((int) image.PixelFormat >> 8) & 0xFF;
}
Upvotes: 1
Reputation: 137148
The comparison is valid, but you are just comparing the value of the enumeration so the result will depend on the order the values were defined in.
If you need to compare the formats of the images you will have to build up the rules yourself. Which value is "greater than" another will depend on your application.
As @Oded suggests in his comment you could create a Dictionary
keyed with the PixelFormat
that returns the correct relative values (colour depth) for your comparisons to return sensible results and use that for your tests.
Upvotes: 1