2

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 ?

H H
  • 263,252
  • 30
  • 330
  • 514
user257412
  • 724
  • 2
  • 9
  • 22
  • Not that it answers your question. But confusing that you would use X to iterate over the vertical position, and y over the horizontal – Toad Feb 16 '10 at 10:03
  • To continue on that thought: since you are using x and y correctly in the DrawImageUnscaled call, but are using them incorrectly in the loop, I suspect not the whole of your picture will be filled – Toad Feb 16 '10 at 10:04
  • nitpick3: I can not imagine that DarImageUnscaled would use it's coordinate system starting with 1. So your loops should really start with 0 – Toad Feb 16 '10 at 10:06
  • i was just trying something different. – user257412 Feb 16 '10 at 10:07

5 Answers5

4

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
  • 107,303
  • 21
  • 270
  • 254
4

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
  • 19,662
  • 12
  • 82
  • 106
  • thanks a lot! . but is it me or is getting a the same 'random' everytime a little illogical – user257412 Feb 16 '10 at 10:08
  • 1
    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 Feb 16 '10 at 10:18
1

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
  • 36,396
  • 8
  • 69
  • 90
0

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!

Christopher B. Adkins
  • 3,499
  • 2
  • 26
  • 29
0

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.

James
  • 9,774
  • 5
  • 34
  • 58