AForge.Imaging.Image.SetGrayscalePalette C# (CSharp) Method

SetGrayscalePalette() public static method

Set pallete of the 8 bpp indexed image to grayscale.
The method initializes palette of Format8bppIndexed image with 256 gradients of gray color.
Provided image is not 8 bpp indexed image.
public static SetGrayscalePalette ( Bitmap image ) : void
image System.Drawing.Bitmap Image to initialize.
return void
        public static void SetGrayscalePalette( Bitmap image )
        {
            // check pixel format
            if ( image.PixelFormat != PixelFormat.Format8bppIndexed )
                throw new UnsupportedImageFormatException( "Source image is not 8 bpp image." );

            // get palette
            ColorPalette cp = image.Palette;
            // init palette
            for ( int i = 0; i < 256; i++ )
            {
                cp.Entries[i] = Color.FromArgb( i, i, i );
            }
            // set palette back
            image.Palette = cp;
        }

Usage Example

Example #1
0
        public unsafe Bitmap ToManagedImage(bool makeCopy)
        {
            Bitmap bitmap = null;

            try
            {
                if (!makeCopy)
                {
                    bitmap = new Bitmap(width, height, stride, pixelFormat, imageData);
                    if (pixelFormat == PixelFormat.Format8bppIndexed)
                    {
                        Image.SetGrayscalePalette(bitmap);
                    }
                }
                else
                {
                    bitmap = ((pixelFormat == PixelFormat.Format8bppIndexed) ? Image.CreateGrayscaleImage(width, height) : new Bitmap(width, height, pixelFormat));
                    BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, pixelFormat);
                    int        num        = bitmapData.Stride;
                    int        count      = System.Math.Min(stride, num);
                    byte *     ptr        = (byte *)bitmapData.Scan0.ToPointer();
                    byte *     ptr2       = (byte *)imageData.ToPointer();
                    if (stride != num)
                    {
                        for (int i = 0; i < height; i++)
                        {
                            SystemTools.CopyUnmanagedMemory(ptr, ptr2, count);
                            ptr  += num;
                            ptr2 += stride;
                        }
                    }
                    else
                    {
                        SystemTools.CopyUnmanagedMemory(ptr, ptr2, stride * height);
                    }
                    bitmap.UnlockBits(bitmapData);
                }
                return(bitmap);
            }
            catch (Exception)
            {
                bitmap?.Dispose();
                throw new InvalidImagePropertiesException("The unmanaged image has some invalid properties, which results in failure of converting it to managed image.");
            }
        }
All Usage Examples Of AForge.Imaging.Image::SetGrayscalePalette