var visual = new DrawingVisual(); using (var dc = visual.RenderOpen()) { var rect = new Rect(10, 10, 100, 100); var brush = new SolidColorBrush(Colors.Red); dc.DrawRectangle(brush, null, rect); } visualHost.Children.Add(visual);
var visual = new DrawingVisual(); using (var dc = visual.RenderOpen()) { SolidColorBrush black = new SolidColorBrush(Colors.Black); SolidColorBrush white = new SolidColorBrush(Colors.White); int width = 500; int height = 500; int size = 50; for (int y = 0; y < height; y += size) { for (int x = 0; x < width; x += size) { bool isWhite = ((y / size) + (x / size)) % 2 == 0; var brush = isWhite ? white : black; var rect = new Rect(x, y, size, size); dc.DrawRectangle(brush, null, rect); } } } visualHost.Children.Add(visual);These examples utilize the System.Windows.Media namespace, which is part of the PresentationCore package library.