tags:

views:

428

answers:

5

Hi,

I have a 3rd party component which requires me to give it the bitsperpixel from a bitmap.

whats the best way to get "bits per pixel"?

I have a c# windows app and my starting point is the following blank method:-

public int GetBitsPerPixelMethod( system.drawing.bitmap bitmap )
{

   //return BitsPerPixel;
}

At the momement I dont have any fancy librarys installed such as directx or anything - but am open to suggestions.

thanks, jason

+1  A: 

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:

switch(Pixelformat)       
  {
     ...
     case Format8bppIndexed:
        BitsPerPixel = 8;
        break;
     case Format24bppRgb:
        BitsPerPixel = 24;
        break;
     case Format32bppArgb:
     case Format32bppPArgb:
     ...
        BitsPerPixel = 32;
        break;
     default:
        BitsPerPixel = 0;
        break;      
 }
schnaader
A: 

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:

var bitsPerPixel = new Dictionary<PixelFormat,int>() {
 { PixelFormat.Format1bppIndexed, 1 },
 { PixelFormat.Format4bppIndexed, 4 },
 { PixelFormat.Format8bppIndexed, 8 },
 { PixelFormat.Format16bppRgb565, 16 }
 /* etc. */
};

return bitsPerPixel[bitmap.PixelFormat];
IRBMe
A: 

What about Image.GetPixelFormatSize()?

Scott
+2  A: 

Rather than creating your own function, I'd suggest using this existing function in the framework:

Image.GetPixelFormatSize(bitmap.PixelFormat)
Ben Daniel