Why doesn't this work?
-
protected override void OnRender(DrawingContext drawCtx) { // Do parent rendering. base.OnRender(drawCtx); // Add our custom rendering. Rect rect = new Rect(); rect.Width = rectWidth; rect.Height = rectHeight; drawCtx.DrawRectangle(Brushes.LightBlue, new Pen(Brushes.Blue, 5), rect); Random rnd = new Random(); SolidColorBrush brush = new SolidColorBrush(); Pen p = new Pen(); byte red, green, blue; Point start = new Point(); Point end = new Point(); for (int i = 0; i < 100; i++) { red = (byte)rnd.Next(0, 255); green = (byte)rnd.Next(0, 255); blue = (byte)rnd.Next(0, 255); brush.Color = Color.FromArgb(255, red, green, blue); start.X = rnd.Next(0, 200); start.Y = rnd.Next(0, 200); end.X = rnd.Next(0, 200); end.Y = rnd.Next(0, 200); drawCtx.DrawLine(new Pen(brush, 3), start, end); //Color.FromArgb(255, red, green, blue), 5.0 } }
When I try this, I get 100 random lines on my screen like I wanted, but the colors of the lines are all the same instead of a new color for every line, so for some reason my code doesn't change the color of the brush. I wouldn't have a clue what could be the reason for this. The rectangle is drawn fine, I added the code for the lines later myself. Would someone be able to help me? Thankyou, Ranger.
-
protected override void OnRender(DrawingContext drawCtx) { // Do parent rendering. base.OnRender(drawCtx); // Add our custom rendering. Rect rect = new Rect(); rect.Width = rectWidth; rect.Height = rectHeight; drawCtx.DrawRectangle(Brushes.LightBlue, new Pen(Brushes.Blue, 5), rect); Random rnd = new Random(); SolidColorBrush brush = new SolidColorBrush(); Pen p = new Pen(); byte red, green, blue; Point start = new Point(); Point end = new Point(); for (int i = 0; i < 100; i++) { red = (byte)rnd.Next(0, 255); green = (byte)rnd.Next(0, 255); blue = (byte)rnd.Next(0, 255); brush.Color = Color.FromArgb(255, red, green, blue); start.X = rnd.Next(0, 200); start.Y = rnd.Next(0, 200); end.X = rnd.Next(0, 200); end.Y = rnd.Next(0, 200); drawCtx.DrawLine(new Pen(brush, 3), start, end); //Color.FromArgb(255, red, green, blue), 5.0 } }
When I try this, I get 100 random lines on my screen like I wanted, but the colors of the lines are all the same instead of a new color for every line, so for some reason my code doesn't change the color of the brush. I wouldn't have a clue what could be the reason for this. The rectangle is drawn fine, I added the code for the lines later myself. Would someone be able to help me? Thankyou, Ranger.
The problem probably comes from the fact that you are using the same brush instance for all the pens you create in the for loop. This:
brush.Color = Color.FromArgb(255, red, green, blue);
will change the color, but you have the same brush each time through the loop. So this:
drawCtx.DrawLine(new Pen(brush, 3), start, end);
will create 100 pens that all use the same brush.
-
The problem probably comes from the fact that you are using the same brush instance for all the pens you create in the for loop. This:
brush.Color = Color.FromArgb(255, red, green, blue);
will change the color, but you have the same brush each time through the loop. So this:
drawCtx.DrawLine(new Pen(brush, 3), start, end);
will create 100 pens that all use the same brush.