Convert Image to Black & White

Convert Image to Black & White

Description:
This post explain how to converts an image to black and white 1bpp indexed format

Code:
        System.Drawing.Drawing2D

        private static Bitmap ConvertToBW(Bitmap src, int luminanceCutOff)
        {
            int width, height;
            Bitmap dest;
            Rectangle rect;
            BitmapData data;
            IntPtr pixels;
            uint row, col;

            //Collect SOURCE Bitmap info
            width = src.Width;
            height = src.Height;

            //Create the DESTINATION Bitmap
            dest = new Bitmap(width, height, PixelFormat.Format1bppIndexed);
            dest.SetResolution(src.HorizontalResolution, src.VerticalResolution);

            //LOCK the Entire Bitmap & get the pixel pointer
            rect = new Rectangle(0, 0, width, height);
            data = dest.LockBits(rect, ImageLockMode.WriteOnly,
                PixelFormat.Format1bppIndexed);
            pixels = data.Scan0;

            unsafe
            {
                Color srcPixel;
                byte* pBits, pDestPixel;
                byte bMask;
                double luminance;

                //Init pointer to the Bits
                if (data.Stride > 0) pBits = (byte*)pixels.ToPointer();
                else pBits = (byte*)pixels.ToPointer() + data.Stride * (height -
                         1);

                //Stride could be negative
                uint stride = (uint)Math.Abs(data.Stride);

                //Go through all the Pixels in the rectangle
                for (row = 0; row < height; row++)
                {
                    for (col = 0; col < width; col++)
                    {
                        //Get the Pixel from the SOURCE
                        srcPixel = src.GetPixel((int)col, (int)row);
                        //srcPixel = Color.White;

                        //Move the DESTINATION to the correct Address / Pointer & get Pixel
                        pDestPixel = pBits + (row * stride) + ((int)(col / 8));

                        //Determine which Bit Represents this Pixel in 1bpp format
                        bMask = (byte)(0x0080 >> (int)(col % 8));

                        //Calculate LUMINANCE to help determine if black or white pixel
                        luminance = (srcPixel.R * 0.299) + (srcPixel.G * 0.587) + (srcPixel.B
                            * 0.114);

                        //Set to Black or White using the Color. Luminance Cut Off
                        if (luminance >= luminanceCutOff)
                            *pDestPixel |= (byte)bMask;        // Set Bit to 1    - White
                        else
                            *pDestPixel &= (byte)~bMask;        // Set Bit to 0 - Black
                    }
                }

            }
            src.Dispose();
            dest.UnlockBits(data);
            return dest;
        }
Leave a Comment
  • Please add 3 and 5 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Comments
Page 1 of 1 (1 items)
Wikis - Comment List
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
Page 1 of 1 (1 items)