Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. Why doesn't this work?

Why doesn't this work?

Scheduled Pinned Locked Moved WCF and WF
helpquestionlounge
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ranger49
    wrote on last edited by
    #1
    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.

    G 1 Reply Last reply
    0
    • R Ranger49
      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.

      G Offline
      G Offline
      Gideon Engelberth
      wrote on last edited by
      #2

      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.

      R 1 Reply Last reply
      0
      • G Gideon Engelberth

        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.

        R Offline
        R Offline
        Ranger49
        wrote on last edited by
        #3

        Thankyou, It works now. A Brush isn't like a class which you have to distruct first before you can create a new instance. Thanks again, Ranger.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups