SoFunction
Updated on 2025-03-06

Detailed explanation of the implementation code of parsing C# color image grayscale algorithm


        public static Bitmap MakeGrayscale(Bitmap original)
        {
            //create a blank bitmap the same size as original
            Bitmap newBitmap = new Bitmap(, );
            //get a graphics object from the new image
            Graphics g = (newBitmap);
            //create the grayscale ColorMatrix
            colorMatrix = new (
               new float[][]
              {
                 new float[] {.3f, .3f, .3f, 0, 0},
                 new float[] {.59f, .59f, .59f, 0, 0},
                 new float[] {.11f, .11f, .11f, 0, 0},
                 new float[] {0, 0, 0, 1, 0},
                 new float[] {0, 0, 0, 0, 1}
              });
            //create some image attributes
            attributes = new ();
            //set the color matrix attribute
            (colorMatrix);
            //draw the original image on the new image
            //using the grayscale color matrix
            (original, new Rectangle(0, 0, , ),
               0, 0, , , , attributes);
            //dispose the Graphics object
            ();
            return newBitmap;
        }