0

I am coding a program that is generating binary(black n white) image . The ARGB(Alpha,Red,Green,Blue) for the white color is(0,255,255,255) and (0,0,0,0) for the black color . so I am using Random number to generate the binary bitmap

int a = rnd.Next(256);
int r = rnd.Next(256);
int g = rnd.Next(256);
int b = rnd.Next(256);

but with this range of numbers it will generate a colors image , so I only need Random numbers that are 0 or 255 , no thing between them . Remember it's a binary not a scale-gray image .

Habib
  • 219,104
  • 29
  • 407
  • 436

2 Answers2

7

The simplest way is:

int a = 255;
int r = rnd.Next(2) * 255;
int g = r;
int b = r;

Or you can achieve the same via:

Color color = rnd.Next(2) == 0 ? Color.Black : Color.White;
Dmitry
  • 13,797
  • 6
  • 32
  • 48
1

Thank you all guys :) this way has worked with me :-

for (int y = 0; y < height; y++)
{
    for (int x = 0; x < width; x++)
    {
        pixel_color = rnd.Next(0, 2);
        if (pixel_color == 0)
        {
            r = 0;
            g = 0;
            b = 0;
        }
        else
        {
            r = 255;
            g = 255;
            b = 255;
        }
        bmp.SetPixel(x, y, Color.FromArgb(r, g, b));
    }
}
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
  • Instead of posting essentially duplicate answer you should commented/voted/accepted existing answer. At very least you should show updating raw bits of bitmap as `SetPixel` is one of the slowest function you can use. – Alexei Levenkov Oct 24 '14 at 06:03