creating-graphics-in-code
Creates WPF graphics dynamically in C# code using Shape, PathGeometry, and PathFigure classes. Use when building charts, diagrams, or procedurally generated graphics.
What this skill does
# Creating WPF Graphics in Code
Programmatically create shapes and geometry for dynamic graphics.
## 1. Shape Factory Pattern
```csharp
namespace MyApp.Graphics;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
public static class ShapeFactory
{
/// <summary>
/// Create circular marker
/// </summary>
public static Ellipse CreateCircle(double size, Brush fill, Brush? stroke = null)
{
var ellipse = new Ellipse
{
Width = size,
Height = size,
Fill = fill
};
if (stroke is not null)
{
ellipse.Stroke = stroke;
ellipse.StrokeThickness = 1;
}
return ellipse;
}
/// <summary>
/// Create rectangle with optional corner radius
/// </summary>
public static Rectangle CreateRectangle(
double width,
double height,
Brush fill,
double cornerRadius = 0)
{
return new Rectangle
{
Width = width,
Height = height,
Fill = fill,
RadiusX = cornerRadius,
RadiusY = cornerRadius
};
}
/// <summary>
/// Create line between two points
/// </summary>
public static Line CreateLine(Point start, Point end, Brush stroke, double thickness = 1)
{
return new Line
{
X1 = start.X,
Y1 = start.Y,
X2 = end.X,
Y2 = end.Y,
Stroke = stroke,
StrokeThickness = thickness
};
}
}
```
---
## 2. Geometry Builder Pattern
```csharp
namespace MyApp.Graphics;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
public static class GeometryBuilder
{
/// <summary>
/// Create polygon from points
/// </summary>
public static PathGeometry CreatePolygon(IReadOnlyList<Point> points)
{
if (points.Count < 3)
return new PathGeometry();
var figure = new PathFigure
{
StartPoint = points[0],
IsClosed = true,
IsFilled = true
};
for (var i = 1; i < points.Count; i++)
{
figure.Segments.Add(new LineSegment(points[i], isStroked: true));
}
var geometry = new PathGeometry();
geometry.Figures.Add(figure);
return geometry;
}
/// <summary>
/// Create polyline (open path)
/// </summary>
public static PathGeometry CreatePolyline(IReadOnlyList<Point> points)
{
if (points.Count < 2)
return new PathGeometry();
var figure = new PathFigure
{
StartPoint = points[0],
IsClosed = false
};
for (var i = 1; i < points.Count; i++)
{
figure.Segments.Add(new LineSegment(points[i], isStroked: true));
}
var geometry = new PathGeometry();
geometry.Figures.Add(figure);
return geometry;
}
/// <summary>
/// Create cubic Bezier curve
/// </summary>
public static PathGeometry CreateBezierCurve(
Point start,
Point control1,
Point control2,
Point end)
{
var figure = new PathFigure { StartPoint = start };
figure.Segments.Add(new BezierSegment(control1, control2, end, isStroked: true));
var geometry = new PathGeometry();
geometry.Figures.Add(figure);
return geometry;
}
/// <summary>
/// Create arc
/// </summary>
public static PathGeometry CreateArc(
Point start,
Point end,
Size size,
SweepDirection direction = SweepDirection.Clockwise)
{
var figure = new PathFigure { StartPoint = start };
figure.Segments.Add(new ArcSegment(end, size, 0, false, direction, isStroked: true));
var geometry = new PathGeometry();
geometry.Figures.Add(figure);
return geometry;
}
}
```
---
## 3. Arrow Creation
```csharp
/// <summary>
/// Create arrow with head
/// </summary>
public static Path CreateArrow(Point start, Point end, Brush stroke, double headSize = 10)
{
var geometry = new PathGeometry();
// Arrow body
var bodyFigure = new PathFigure { StartPoint = start };
bodyFigure.Segments.Add(new LineSegment(end, isStroked: true));
geometry.Figures.Add(bodyFigure);
// Calculate arrow head direction
var direction = end - start;
direction.Normalize();
var perpendicular = new Vector(-direction.Y, direction.X);
var headBase = end - direction * headSize;
var headLeft = headBase + perpendicular * (headSize / 2);
var headRight = headBase - perpendicular * (headSize / 2);
// Arrow head
var headFigure = new PathFigure { StartPoint = end, IsClosed = true, IsFilled = true };
headFigure.Segments.Add(new LineSegment(headLeft, isStroked: true));
headFigure.Segments.Add(new LineSegment(headRight, isStroked: true));
geometry.Figures.Add(headFigure);
return new Path
{
Data = geometry,
Stroke = stroke,
Fill = stroke,
StrokeThickness = 2
};
}
```
---
## 4. StreamGeometry (Performance)
```csharp
/// <summary>
/// Create optimized geometry using StreamGeometry
/// </summary>
public static StreamGeometry CreateOptimizedPolygon(IReadOnlyList<Point> points)
{
var geometry = new StreamGeometry();
using (var context = geometry.Open())
{
context.BeginFigure(points[0], isFilled: true, isClosed: true);
for (var i = 1; i < points.Count; i++)
{
context.LineTo(points[i], isStroked: true, isSmoothJoin: false);
}
}
geometry.Freeze(); // Make immutable for performance
return geometry;
}
/// <summary>
/// Create optimized smooth curve
/// </summary>
public static StreamGeometry CreateOptimizedCurve(IReadOnlyList<Point> points)
{
var geometry = new StreamGeometry();
using (var context = geometry.Open())
{
context.BeginFigure(points[0], isFilled: false, isClosed: false);
context.PolyBezierTo(points.Skip(1).ToList(), isStroked: true, isSmoothJoin: true);
}
geometry.Freeze();
return geometry;
}
```
---
## 5. Adding to Canvas
```csharp
public void AddShapesToCanvas(Canvas canvas, IReadOnlyList<DataPoint> data)
{
canvas.Children.Clear();
foreach (var point in data)
{
var marker = ShapeFactory.CreateCircle(10, Brushes.Blue);
Canvas.SetLeft(marker, point.X - 5);
Canvas.SetTop(marker, point.Y - 5);
canvas.Children.Add(marker);
}
// Add connecting line
var linePoints = data.Select(d => new Point(d.X, d.Y)).ToList();
var linePath = new Path
{
Data = GeometryBuilder.CreatePolyline(linePoints),
Stroke = Brushes.Blue,
StrokeThickness = 2
};
canvas.Children.Add(linePath);
}
```
---
## 6. Performance Guidelines
| Approach | Use Case | Performance |
|----------|----------|-------------|
| **Shape** | Interactive elements | Lower (full layout) |
| **PathGeometry** | Complex dynamic shapes | Medium |
| **StreamGeometry** | Static complex shapes | Higher |
| **DrawingVisual** | Large datasets | Highest |
**Always call `Freeze()`** on geometry, brushes, and pens for static graphics.
---
## 7. References
- [Geometry Overview - Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/geometry-overview)
- [StreamGeometry - Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.streamgeometry)
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.