2009-07-14 63 views
19

我有一个第三方组件,它要求我从位图中为它提供bitsperpixel。如何从位图中获取Bitsperpixel

获得“每像素位数”的最佳方法是什么?

我的出发点是在下面的空格方法: -

public int GetBitsPerPixelMethod(system.drawing.bitmap bitmap) 
{ 
    //return BitsPerPixel; 
} 

回答

1

使用Pixelformat property,这将返回一个Pixelformat enumeration能有像F.E.值Format24bppRgb,这显然是每像素24位,所以你应该能够做这样的事情:

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

Bitmap.PixelFormat属性会告诉你,位图具有像素格式的类型,并从可以推断每像素的位数。我不知道是否有收到这个更好的方法,但用简单的方式至少会是这样的:

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

return bitsPerPixel[bitmap.PixelFormat]; 
1

怎么样Image.GetPixelFormatSize()?

73

而不是创建自己的功能,我建议在框架中使用此功能存在:

Image.GetPixelFormatSize(bitmap.PixelFormat) 
+6

这应该是这个问题的接受答案 – 2016-02-13 23:20:58

+1

这并且只有这应该是答案。 谢谢你分享这个。 – datoml 2017-10-05 07:03:29