In C#, WPF I've created a rectangle:

        Rectangle myRgbRectangle = new Rectangle();
        myRgbRectangle.Width = 1;
        myRgbRectangle.Height = 1;
        SolidColorBrush mySolidColorBrush = new SolidColorBrush();

Yes, I really just want it to be 1 pixel by 1 pixel. And I want to change the color based on the variable height like so:

        mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, (byte)height);
        myRgbRectangle.Fill = mySolidColorBrush;

Now, how do I draw at a specific x,y location on the screen? I do have a grid (myGrid) on my MainWindow.xaml.

Thanks!


Here's the pertinent code:

        myRgbRectangle.Width = 1;
        myRgbRectangle.Height = 1;
        SolidColorBrush mySolidColorBrush = new SolidColorBrush();

        int height;
        for (int i = 0; i < ElevationManager.Instance.heightData.GetLength(0); i++)
            for (int j = 0; j < ElevationManager.Instance.heightData.GetLength(1); j++)
            {
                height = ElevationManager.Instance.heightData[i, j] / 100;
                // Describes the brush's color using RGB values. 
                // Each value has a range of 0-255.
                mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, (byte)height);
                myRgbRectangle.Fill = mySolidColorBrush;

                myCanvas.Children.Add(myRgbRectangle);
                Canvas.SetTop(myRgbRectangle, j);
                Canvas.SetLeft(myRgbRectangle, i);

And it's throwing this error: Specified Visual is already a child of another Visual or the root of a CompositionTarget.

Edit
Report