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
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.");
            }
        }
Example #2
0
        /// <summary>
        /// Create managed image from the unmanaged.
        /// </summary>
        ///
        /// <param name="makeCopy">Make a copy of the unmanaged image or not.</param>
        ///
        /// <returns>Returns managed copy of the unmanaged image.</returns>
        ///
        /// <remarks><para>If the <paramref name="makeCopy"/> is set to <see langword="true"/>, then the method
        /// creates a managed copy of the unmanaged image, so the managed image stays valid even when the unmanaged
        /// image gets disposed. However, setting this parameter to <see langword="false"/> creates a managed image which is
        /// just a wrapper around the unmanaged image. So if unmanaged image is disposed, the
        /// managed image becomes no longer valid and accessing it will generate an exception.</para></remarks>
        ///
        /// <exception cref="InvalidImagePropertiesException">The unmanaged image has some invalid properties, which results
        /// in failure of converting it to managed image. This may happen if user used the
        /// <see cref="UnmanagedImage(IntPtr, int, int, int, PixelFormat)"/> constructor specifying some
        /// invalid parameters.</exception>
        ///
        public Bitmap ToManagedImage(bool makeCopy)
        {
            Bitmap dstImage = null;

            try
            {
                if (!makeCopy)
                {
                    dstImage = new Bitmap(width, height, stride, pixelFormat, imageData);
                    if (pixelFormat == PixelFormat.Format8bppIndexed)
                    {
                        Image.SetGrayscalePalette(dstImage);
                    }
                }
                else
                {
                    // create new image of required format
                    dstImage = (pixelFormat == PixelFormat.Format8bppIndexed) ?
                               AForge.Imaging.Image.CreateGrayscaleImage(width, height) :
                               new Bitmap(width, height, pixelFormat);

                    // lock destination bitmap data
                    BitmapData dstData = dstImage.LockBits(
                        new Rectangle(0, 0, width, height),
                        ImageLockMode.ReadWrite, pixelFormat);

                    int dstStride = dstData.Stride;
                    int lineSize  = Math.Min(stride, dstStride);

                    unsafe
                    {
                        byte *dst = (byte *)dstData.Scan0.ToPointer( );
                        byte *src = (byte *)imageData.ToPointer( );

                        if (stride != dstStride)
                        {
                            // copy image
                            for (int y = 0; y < height; y++)
                            {
                                AForge.SystemTools.CopyUnmanagedMemory(dst, src, lineSize);
                                dst += dstStride;
                                src += stride;
                            }
                        }
                        else
                        {
                            AForge.SystemTools.CopyUnmanagedMemory(dst, src, stride * height);
                        }
                    }

                    // unlock destination images
                    dstImage.UnlockBits(dstData);
                }

                return(dstImage);
            }
            catch (Exception)
            {
                if (dstImage != null)
                {
                    dstImage.Dispose( );
                }

                throw new InvalidImagePropertiesException("The unmanaged image has some invalid properties, which results in failure of converting it to managed image.");
            }
        }