0

I need to convert black image to greyscale image.my code convert color image to greyscale image.but won't convert black image to greyscale image.Even try one sample from stackoverflow for greyscale conversion .that also not working for black to grey scale image conversion. that link is https://stackoverflow.com/a/2265990/8569792

public void GreyScaleConversion()
{          
    //read image
     Bitmap bmp = global::Ribbon.Properties.Resources.Device;

     //load original image in picturebox1
     pictureBox1.Image = global::Ribbon.Properties.Resources.Device;

     //get image dimension
     int width = bmp.Width;
     int height = bmp.Height;

     //color of pixel
     Color p;

     //grayscale
     for (int y = 0; y < height; y++)
     {
         for (int x = 0; x < width; x++)
         {
             //get pixel value
             p = bmp.GetPixel(x, y);

             //extract pixel component ARGB
             int a = p.A;
             int r = p.R;
             int g = p.G;
             int b = p.B;

             //find average
             int avg = (r + g + b) / 3;

             //set new pixel value
             bmp.SetPixel(x, y, Color.FromArgb(a, avg, avg, avg));
         }
     }

     //load grayscale image in picturebox2
     pictureBox2.Image = bmp;
}
Vignesh Nethaji
  • 374
  • 3
  • 15

1 Answers1

0
    public void GreyScaleConversion()
    {
        //read image
        Bitmap bmp = global::Ribbon.Properties.Resources.Device;

        //load original image in picturebox1
        pictureBox1.Image = global::Ribbon.Properties.Resources.Device;

        //get image dimension
        int width = bmp.Width;
        int height = bmp.Height;

        //color of pixel
        Color p;

        //grayscale
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                //get pixel value
                p = bmp.GetPixel(x, y);

                //extract pixel component A
                int a = p.A;

                //set new pixel value
                bmp.SetPixel(x, y, Color.FromArgb(a, 155, 155, 155));
            }
        }

        //load grayscale image in picturebox2
        pictureBox2.Image = bmp;
    }
Vignesh Nethaji
  • 374
  • 3
  • 15