--- name: implementing-2d-graphics description: "Implements WPF 2D graphics using Shape, Geometry, Brush, and Pen classes. Use when building vector graphic UIs, icons, charts, or diagrams in WPF applications." --- # WPF 2D Graphics Patterns Implement vector-based visual elements using WPF's 2D graphics system. ## 1. Graphics Hierarchy ``` UIElement └── Shape (FrameworkElement) ← Participates in layout, supports events ├── Ellipse ├── Rectangle ├── Line ├── Polyline ├── Polygon └── Path Drawing ← Lightweight, no events ├── GeometryDrawing ├── ImageDrawing ├── VideoDrawing └── GlyphRunDrawing ``` --- ## 2. Shape Basics ### 2.1 Basic Shapes ```xml ``` ### 2.2 Path and Geometry ```xml ``` ### 2.3 Path Mini-Language | Command | Description | Example | |---------|-------------|---------| | **M** | MoveTo (start point) | M 10,10 | | **L** | LineTo (straight line) | L 100,100 | | **H** | Horizontal LineTo | H 100 | | **V** | Vertical LineTo | V 100 | | **A** | ArcTo (arc) | A 50,50 0 0 1 100,100 | | **C** | Cubic Bezier | C 20,20 40,60 100,100 | | **Q** | Quadratic Bezier | Q 50,50 100,100 | | **Z** | ClosePath | Z | Lowercase = relative coordinates, Uppercase = absolute coordinates --- ## 3. Geometry ### 3.1 Basic Geometry ```xml ``` ### 3.2 CombinedGeometry (Shape Combination) ```xml ``` **GeometryCombineMode:** - **Union**: Union of shapes - **Intersect**: Intersection - **Exclude**: Difference (Geometry1 - Geometry2) - **Xor**: Exclusive union ### 3.3 GeometryGroup (Multiple Geometry) ```xml ``` **FillRule:** - **EvenOdd**: Even-odd rule (donut shape) - **Nonzero**: Non-zero rule (filled) --- ## 4. Brush (Quick Reference) ```xml ``` > **📌 상세 가이드**: `/creating-wpf-brushes` skill 참조 --- ## 5. Stroke Styling ### 5.1 StrokeDashArray ```xml ``` ### 5.2 StrokeLineCap / StrokeLineJoin ```xml ``` **LineCap:** Flat, Round, Square, Triangle **LineJoin:** Miter, Bevel, Round --- ## 6. Related Skills | Skill | Description | |-------|-------------| | `/creating-wpf-brushes` | All Brush patterns (Solid, Linear, Radial, Image, Visual) | | `/creating-wpf-vector-icons` | XAML vector icons, IconButton styles | | `/creating-graphics-in-code` | C# dynamic graphics (ShapeFactory, GeometryBuilder) | --- ## 7. Performance Considerations | Element | Complexity | Recommended Use | |---------|------------|-----------------| | **Shape** | High | Interactive elements (click, drag) | | **DrawingVisual** | Low | Large static graphics | | **StreamGeometry** | Lowest | Fixed complex paths | ```csharp // StreamGeometry: immutable, optimized var streamGeometry = new StreamGeometry(); using (var context = streamGeometry.Open()) { context.BeginFigure(new Point(0, 0), isFilled: true, isClosed: true); context.LineTo(new Point(100, 0), isStroked: true, isSmoothJoin: false); context.LineTo(new Point(100, 100), isStroked: true, isSmoothJoin: false); } streamGeometry.Freeze(); // Set immutable for performance improvement ``` --- ## 8. References - [Shapes and Basic Drawing - Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/shapes-and-basic-drawing-in-wpf-overview) - [Geometry Overview - Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/geometry-overview) - [Painting with Brushes - Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/painting-with-solid-colors-and-gradients-overview)