how to get Bitsperpixel from a bitmap

后端 未结 6 1981
心在旅途
心在旅途 2021-02-18 12:41

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 th

6条回答
  •  渐次进展
    2021-02-18 13:13

    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.Format1bppIndexed, 1 },
        { PixelFormat.Format4bppIndexed, 4 },
        { PixelFormat.Format8bppIndexed, 8 },
        { PixelFormat.Format16bppRgb565, 16 }
        /* etc. */
    };
    
    return bitsPerPixel[bitmap.PixelFormat];
    

提交回复
热议问题