2009-07-14 49 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