如何从位图获取Bitsperpixel

jas*_*ark 20 c# drawing

我有一个第三方组件,它要求我从位图给它bitsperpixel.

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

我的出发点是以下空白方法: -

public int GetBitsPerPixelMethod( system.drawing.bitmap bitmap )
{
   //return BitsPerPixel;
}
Run Code Online (Sandbox Code Playgroud)

Cod*_*ats 89

我建议在框架中使用这个现有函数,而不是创建自己的函数:

Image.GetPixelFormatSize(bitmap.PixelFormat)
Run Code Online (Sandbox Code Playgroud)

  • 这应该是这个问题的公认答案 (11认同)
  • 这只应该是答案.感谢您分享这一点. (2认同)

小智 7

var source = new BitmapImage(new System.Uri(pathToImageFile));
int bitsPerPixel = source.Format.BitsPerPixel;
Run Code Online (Sandbox Code Playgroud)

上面的代码至少需要.NET 3.0

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.aspx


sch*_*der 1

使用Pixelformat 属性,这会返回一个Pixelformat 枚举,它可以具有类似 fe 的值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;      
 }
Run Code Online (Sandbox Code Playgroud)