Beispiel #1
0
        private System.Drawing.Bitmap CreateBitmap(int width, int height, int bitsPerPixel, uint[] buf)
        {
            System.Drawing.Bitmap bmp;
            System.Drawing.Imaging.PixelFormat pixelFormat;

            System.Drawing.Color[] colors = null;

            switch (bitsPerPixel)
            {
            case 1:
                pixelFormat = System.Drawing.Imaging.PixelFormat.Format1bppIndexed;
                colors      = new System.Drawing.Color[] { System.Drawing.Color.White, System.Drawing.Color.Black };
                break;

            case 4:
            case 8:
                //Not tested
                int cColors = 1 << bitsPerPixel;

                pixelFormat = (bitsPerPixel == 4) ? System.Drawing.Imaging.PixelFormat.Format4bppIndexed : System.Drawing.Imaging.PixelFormat.Format8bppIndexed;

                colors = new System.Drawing.Color[cColors];

                for (int i = 0; i < cColors; i++)
                {
                    int intensity = 256 / cColors * i;
                    colors[i] = System.Drawing.Color.FromArgb(intensity, intensity, intensity);
                }

                break;

            case 16:
                pixelFormat = System.Drawing.Imaging.PixelFormat.Format16bppRgb565;
                break;

            default:
                throw new Exception("The bit depth specified, " + bitsPerPixel + ", is not supported.");
            }

            bmp = new System.Drawing.Bitmap(width, height, pixelFormat);

            if (colors != null)
            {
                System.Drawing.Imaging.ColorPalette palette = bmp.Palette;
                colors.CopyTo(palette.Entries, 0);
                bmp.Palette = palette;
            }

            return(bmp);
        }