I have a 3rd party component which requires me to give it the bitsperpixel from a bitmap.
What's the best way to get "bits per pixel"?
My starting point is the following blank method:-
public int GetBitsPerPixelMethod( system.drawing.bitmap bitmap )
{
//return BitsPerPixel;
}
The code above requires at least .NET 3.0
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.aspx
What about Image.GetPixelFormatSize()?
Try:
Bitmap.PixelFormat
See possible values of the PixelFormat property.
The Bitmap.PixelFormat property will tell you the type of pixel format that the bitmap has, and from that you can infer the number of bits per pixel. I'm not sure if there's a better way of getting this, but the naive way at least would be something like this:
Use the Pixelformat property, this returns a Pixelformat enumeration which can have values like f.e.
Format24bppRgb
, which obviously is 24 bits per pixel, so you should be able to do something like this:Rather than creating your own function, I'd suggest using this existing function in the framework: