Renders high-performance graphics using WPF DrawingContext for 10-50x improvement over Shape. Use when drawing large numbers of shapes or optimizing rendering performance.
View on GitHubchristian289/dotnet-with-claudecode
wpf-dev-pack
January 23, 2026
Select agents to install to:
npx add-skill https://github.com/christian289/dotnet-with-claudecode/blob/main/wpf-dev-pack/skills/rendering-with-drawingcontext/SKILL.md -a claude-code --skill rendering-with-drawingcontextInstallation paths:
.claude/skills/rendering-with-drawingcontext/# WPF DrawingContext High-Performance Rendering
A pattern for achieving 10-50x performance improvement over Shape objects when rendering large numbers of shapes in WPF using DrawingContext.
## 1. Core Concepts
### Shape vs DrawingContext Approach
| Item | Shape (Polygon, Rectangle, etc.) | DrawingContext |
|------|----------------------------------|----------------|
| **Inheritance** | Canvas | FrameworkElement |
| **Visual count** | One per shape (n) | 1 |
| **Layout calculation** | O(n) Measure/Arrange | O(1) |
| **Memory usage** | Very high (WPF object overhead) | Very low (data only) |
| **Performance** | Baseline | **10-50x faster** |
| **Suitable for** | Few interactive shapes (tens to hundreds) | Large static shapes (thousands to tens of thousands) |
### Why is DrawingContext Fast?
1. **Single Visual**: Only 1 FrameworkElement registered in Visual Tree
2. **Layout bypass**: No Measure/Arrange calculations needed
3. **Batch rendering**: Sent to GPU as single batch
4. **Memory efficiency**: Only stores shape metadata
---
## 2. Basic Implementation Pattern
### 2.1 DrawingContext-Based Custom Control
```csharp
namespace MyApp.Controls;
using System.Windows;
using System.Windows.Media;
public sealed class HighPerformanceCanvas : FrameworkElement
{
// 1. Struct for storing shape data (lightweight)
private readonly record struct ShapeData(
Point Position,
double Width,
double Height,
Brush Fill);
// 2. Only rendering data stored in memory
private readonly List<ShapeData> _shapes = [];
// 3. Optimized Pen (Freeze applied)
private readonly Pen _pen = new(Brushes.Black, 1);
public HighPerformanceCanvas()
{
// Freeze Pen for performance optimization
_pen.Freeze();
}
// 4. Shape addition method
public void AddShape(Point position, double width, double height, Color color)
{
var brush = new SolidColorBrush(color);
brush.Freeze(); // Freeze for p