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 ?

4
  • Not that it answers your question. But confusing that you would use X to iterate over the vertical position, and y over the horizontal Commented Feb 16, 2010 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 Commented Feb 16, 2010 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 Commented Feb 16, 2010 at 10:06
  • i was just trying something different. Commented Feb 16, 2010 at 10:07

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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). msdn.microsoft.com/en-us/library/system.random.aspx
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.

Comments

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 

4 Comments

look at my comments under the question. There are still incorrect things here.
just to make you happy and annoy anyone looking at the code.... just change it in the draw. dyslexia anyone?
;^) Shouldn't the loops start at 0 though?
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.
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!

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.