views:

203

answers:

5

this creates streaks instead of dots. why ? I am trying to paint individual pixels. another method was also tried (using fillrectangle) , which also didn't give the desired result, got bars instead of dots.

protected override void OnPaint(PaintEventArgs pea )
    {

         Graphics g = pea.Graphics ;

         for ( int y = 1 ; y <= Width ; y++ )
         {
            for (  int x  =  1  ; x <=  Height    ; x++ )
            {
                System.Random rand = new  System.Random() ;
                Color c =   (Color.FromArgb(rand.Next(256),
                                    rand.Next(256),
                                    rand.Next(256)));
            // SolidBrush myBrush = new SolidBrush(c);
            // g.FillRectangle(myBrush, x, y, 1, 1);


                 Bitmap pt = new Bitmap(1, 1);
                 pt.SetPixel(0, 0, c);
                 g.DrawImageUnscaled(pt, x, y);

            }
        }

}

what is happening here ?

+4  A: 

You need to create the System.Random object outside the loops. When initialized inside the loop it keep getting the same seed (since it's initialized according to the system clock, which would still have the same value) and therefore the same color.

interjay
+3  A: 

You should not create a new Random object each time. Since that will give you the exact same "random" numbers repeated over and over.

Instead use the same Random object and just call Next on in.

Martin Wickman
thanks a lot! . but is it me or is getting a the same 'random' everytime a little illogical
Random numbers are generated from a starting value, called the "seed". If you use the same seed every time, you will also get the exact sequence of random numbers each time. Quite possibly you are using the same seed here (or it's using the system clock which doesn't have time to change much inside the quick loop). http://msdn.microsoft.com/en-us/library/system.random.aspx
Martin Wickman
hmmm, thanks for explaining!
+1  A: 

Yeah, what they said.

and you might get better mileage bitblitting something like that. e.g. draw it offscreen and then paint it all at once.

protected override void OnPaint(PaintEventArgs pea)
{
    using (var b = new Bitmap(this.Width, this.Height))
    {
        using (var g = Graphics.FromImage(b))
        {
            var rand = new Random();

            for (int x = 0; x <= Width; x++)
            {
                for (int y = 0; y <= Height; y++)
                {
                    Color c = (Color.FromArgb(rand.Next(256),
                                              rand.Next(256),
                                              rand.Next(256)));
                    using (var newPen = new Pen(c, 1))
                    {
                        g.DrawRectangle(newPen, x, y, 0.5f, 0.5f);
                    }
                }
            }
        }

        pea.Graphics.DrawImage(b, 0, 0);
    }
}

protected override void OnPaint(PaintEventArgs pea)
{
    Graphics g = pea.Graphics;
    var rand = new Random();

    for (int x = 0; x <= Width; x++)
    {
        for (int y = 0; y <= Height; y++)
        {
            Color c = (Color.FromArgb(rand.Next(256),
                                      rand.Next(256),
                                      rand.Next(256)));
            using (var newPen = new Pen(c, 1))
            {
                g.DrawRectangle(newPen, x, y, 0.5f, 0.5f);
            }
        }
    }
}y
Sky Sanders
look at my comments under the question. There are still incorrect things here.
Toad
just to make you happy and annoy anyone looking at the code.... just change it in the draw. dyslexia anyone?
Sky Sanders
@sky: ;^) Shouldn't the loops start at 0 though?
Toad
Hey, it's not my mess, I just fix the really broken stuff and don't sweat the small stuff. ok... it will become CW very shortly.
Sky Sanders
A: 

You need to move the System.Radom outside of the loop. What is happening here is that you are creating the same random point every time the loop iterates. Also if you add using System; to the start of the program than you can simply say Random (but that is not important just a side note). Once the Random is created you can then get the next number by simply calling Next on the object. Try this out and I believe it will rid you of your problems!

Adkins
A: 

You are drawing a different coloured dot on every pixel in the rectangle 0,0,Width,Height. But as the previous answers state because random is being intialised to the system clock within each loop, the loop is drawing the same colour giving you a streak of the same colour (Horizontally?) until the system clock ticks on a tiny bit and gives you a different seed.

If you move the System.Random outside the loop so you get a different colour each Next then you will see dots.

JamesB