Rectangle rect = new Rectangle(); rect.Width = 100; rect.Height = 50; rect.Fill = Brushes.Red;
Rectangle rect = new Rectangle(); rect.Width = 100; rect.Height = 50; GradientStopCollection gradientStops = new GradientStopCollection(); gradientStops.Add(new GradientStop(Colors.Red, 0.0)); gradientStops.Add(new GradientStop(Colors.Blue, 1.0)); LinearGradientBrush brush = new LinearGradientBrush(gradientStops, new Point(0, 0), new Point(1, 1)); rect.Fill = brush;This code creates a new Rectangle object with a Width of 100 pixels, Height of 50 pixels, and filled with a gradient color that transitions from red to blue. The LinearGradientBrush class is part of the System.Windows.Media namespace. Package Library: The System.Windows.Shapes Rectangle class is part of the core .NET Framework library, which is included with every installation of the .NET Framework.