Get Bits Per Pixel by pixel format - CSharp System.Drawing

CSharp examples for System.Drawing:Bitmap

Description

Get Bits Per Pixel by pixel format

Demo Code


using System.Drawing.Imaging;
using System.Drawing;
using System;//from w ww. j av a 2s .co m

public class Main{
        public static byte GetBitsPerPixel(Bitmap b)
        {
            switch (b.PixelFormat)
            {
                case PixelFormat.Format1bppIndexed:
                    return 1;
                case PixelFormat.Format4bppIndexed:
                    return 4;
                case PixelFormat.Format8bppIndexed:
                    return 8;
                case PixelFormat.Format16bppGrayScale:
                case PixelFormat.Format16bppRgb555:
                case PixelFormat.Format16bppRgb565:
                case PixelFormat.Format16bppArgb1555:
                    return 16;
                case PixelFormat.Format24bppRgb:
                    return 24;
                case PixelFormat.Format32bppRgb:
                case PixelFormat.Format32bppArgb:
                case PixelFormat.Format32bppPArgb:
                    return 32;
                case PixelFormat.Format48bppRgb:
                    return 48;
                case PixelFormat.Format64bppArgb:
                case PixelFormat.Format64bppPArgb:
                    return 64;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
}

Related Tutorials