
Maui Graphics Drawing
- 36 installs
- 163 repo stars
- Updated July 6, 2026
- davidortinau/maui-skills
Covers custom drawing with Microsoft.Maui.Graphics and GraphicsView including shapes, paths, text, images, shadows, clipping, and canvas state.
About
Guides custom drawing with Microsoft.Maui.Graphics and GraphicsView, covering canvas operations, shapes, paths, text and image rendering, shadows, clipping and canvas state. A developer uses it when rendering custom graphics on a MAUI canvas.
- Canvas drawing operations, shapes, and paths
- Text and image rendering, shadows, clipping, and canvas state
Maui Graphics Drawing by the numbers
- 36 all-time installs (skills.sh)
- Ranked #646 of 1,039 Mobile Development skills by installs in the Skillselion catalog
- Data as of Aug 3, 2026 (Skillselion catalog sync)
npx skills add https://github.com/davidortinau/maui-skills --skill maui-graphics-drawingAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 36 |
|---|---|
| repo stars | ★ 163 |
| Last updated | July 6, 2026 |
| Repository | davidortinau/maui-skills ↗ |
What it does
Covers custom drawing with Microsoft.Maui.Graphics and GraphicsView including shapes, paths, text, images, shadows, clipping, and canvas state.
Files
.NET MAUI Graphics Drawing
Common gotchas
| Issue | Fix |
|---|---|
| Nothing draws on screen | Ensure Drawable is set on GraphicsView and the control has non-zero HeightRequest/WidthRequest |
| State bleeds between shapes | Wrap isolated sections in SaveState() / RestoreState() pairs |
| Shadows stick to later draws | Call canvas.SetShadow(SizeF.Zero, 0, null) after drawing the shadowed element |
| Clipping never resets | Clipping is cumulative per frame — use SaveState/RestoreState around clip regions |
| UI freezes during drawing | Never do I/O, network, or heavy computation inside Draw() — it runs on the UI thread |
Canvas state — always pair Save/Restore
⚠️ Unpaired SaveState/RestoreState causes state leaks across draw calls.
// ✅ Correct — isolated state
canvas.SaveState();
canvas.StrokeColor = Colors.Red;
canvas.StrokeSize = 6;
canvas.DrawRectangle(10, 10, 80, 80);
canvas.RestoreState();
// Stroke reverts to previous values
// ❌ Wrong — state leaks to everything drawn after
canvas.StrokeColor = Colors.Red;
canvas.StrokeSize = 6;
canvas.DrawRectangle(10, 10, 80, 80);
// Every subsequent shape is now red with size 6Saves/restores: stroke, fill, font, shadow, clip, and transforms. Nest calls for layered isolation.
Triggering redraws
// ✅ Correct — queue a redraw
graphicsView.Invalidate();
// ❌ Wrong — never call Draw() directly
myDrawable.Draw(canvas, rect);Invalidate()queues a redraw; the framework callsIDrawable.Drawon the next frame.- ⚠️ Avoid calling
Invalidate()in a tight loop — batch state changes, then invalidate once.
Performance tips
- Keep `Draw()` fast — pre-compute paths and data outside the draw method;
Draw()is called on every frame. - Reuse `PathF` objects — create them once, store as fields, draw repeatedly.
- Use `MeasureFirstItem`-style thinking — if drawing many identical items, calculate dimensions once.
- Minimize allocations — avoid
new PathF()insideDraw()when the path doesn't change.
// ✅ Pre-computed path (field)
private readonly PathF _starPath = BuildStarPath();
public void Draw(ICanvas canvas, RectF dirtyRect)
{
canvas.FillPath(_starPath);
}
// ❌ Allocating every frame
public void Draw(ICanvas canvas, RectF dirtyRect)
{
var path = new PathF();
// ...build path every frame...
canvas.FillPath(path);
}Shadows and clipping — sticky state pitfalls
Shadows apply to all subsequent draws until explicitly removed. Clips accumulate and can only be undone with RestoreState():
// ✅ Shadow: remove after use
canvas.SetShadow(new SizeF(5, 5), 4, Colors.Gray);
canvas.FillRectangle(20, 20, 100, 60);
canvas.SetShadow(SizeF.Zero, 0, null); // ← must remove
// ✅ Clip: isolate with SaveState/RestoreState
canvas.SaveState();
canvas.ClipRectangle(20, 20, 100, 100);
canvas.FillRectangle(0, 0, 200, 200); // clipped
canvas.RestoreState(); // clip removed
// ❌ Clip persists — everything after is also clipped
canvas.ClipRectangle(20, 20, 100, 100);
canvas.FillEllipse(150, 150, 50, 50); // unintentionally clipped!Set properties BEFORE draw calls
// ✅ Properties then draw
canvas.StrokeColor = Colors.Blue;
canvas.DrawRectangle(10, 10, 100, 50);
// ❌ Setting after draw has no effect on previous shape
canvas.DrawRectangle(10, 10, 100, 50);
canvas.StrokeColor = Colors.Blue;Decision framework
| Need | Approach |
|---|---|
| Simple shapes / static graphics | Single IDrawable, draw in Draw() |
| Animated graphics | Update state externally, call Invalidate() from timer/animation |
| Complex layered scene | Multiple SaveState/RestoreState blocks, or separate drawables |
| Hit testing on drawn elements | Track shape bounds manually — GraphicsView has no built-in hit test on drawn content |
| Platform-specific rendering | Use handlers/platform code; Microsoft.Maui.Graphics is cross-platform only |
Quick checklist
- [ ]
GraphicsViewhasDrawableset and non-zero size - [ ]
Draw()is fast — no I/O, no heavy allocations - [ ] Every
SaveState()has a matchingRestoreState() - [ ] Shadows removed with
SetShadow(SizeF.Zero, 0, null)after use - [ ] Clips wrapped in
SaveState/RestoreStateblocks - [ ] Properties set before the draw call they apply to
- [ ]
Invalidate()used instead of callingDraw()directly
Microsoft.Maui.Graphics Drawing API Reference
GraphicsView and IDrawable
GraphicsView is the canvas host control. Assign an IDrawable implementation to its Drawable property.
<GraphicsView Drawable="{StaticResource myDrawable}"
HeightRequest="300"
WidthRequest="300" />public class MyDrawable : IDrawable
{
public void Draw(ICanvas canvas, RectF dirtyRect)
{
// All drawing happens here
}
}ICanvasprovides all drawing methods.RectF dirtyRectis the area that needs redrawing.- Call
graphicsView.Invalidate()to trigger a redraw from outside the drawable.
Drawing Shapes
Lines
canvas.StrokeColor = Colors.Blue;
canvas.StrokeSize = 2;
canvas.DrawLine(10, 10, 200, 10);Rectangles
canvas.StrokeColor = Colors.Black;
canvas.DrawRectangle(10, 10, 100, 50);
canvas.FillColor = Colors.LightBlue;
canvas.FillRectangle(10, 10, 100, 50);
canvas.DrawRoundedRectangle(10, 10, 100, 50, 12);
canvas.FillRoundedRectangle(10, 10, 100, 50, 12);Ellipses
canvas.DrawEllipse(10, 10, 100, 80);
canvas.FillEllipse(10, 10, 100, 80);Arcs
canvas.DrawArc(10, 10, 100, 100, 0, 180, clockwise: true, closed: false);Paths with PathF
Use PathF for complex shapes built from segments.
var path = new PathF();
path.MoveTo(10, 10);
path.LineTo(100, 10);
path.LineTo(100, 100);
path.QuadTo(50, 150, 10, 100); // control point, end point
path.CubicTo(0, 80, 0, 40, 10, 10); // cp1, cp2, end
path.Close();
canvas.StrokeColor = Colors.Red;
canvas.DrawPath(path);
canvas.FillColor = Colors.Orange;
canvas.FillPath(path);PathF methods
| Method | Description |
|---|---|
MoveTo(x, y) | Move without drawing |
LineTo(x, y) | Straight line to point |
QuadTo(cx, cy, x, y) | Quadratic Bézier curve |
CubicTo(c1x, c1y, c2x, c2y, x, y) | Cubic Bézier curve |
Close() | Close the path back to the start |
Drawing Text
canvas.FontColor = Colors.Black;
canvas.FontSize = 18;
canvas.Font = Microsoft.Maui.Graphics.Font.Default;
// DrawString with bounding rect and alignment
canvas.DrawString("Hello", 10, 10, 200, 40,
HorizontalAlignment.Center,
VerticalAlignment.Center);
// DrawString at a point (no bounding box)
canvas.DrawString("World", 10, 60, HorizontalAlignment.Left);- Set
FontColor,FontSize, andFontbefore callingDrawString. - Overloads accept either a point or a bounding rectangle with alignment.
Canvas State Properties
Set these properties before draw calls to control appearance.
Stroke
canvas.StrokeColor = Colors.Navy;
canvas.StrokeSize = 4;
canvas.StrokeDashPattern = new float[] { 6, 3 }; // dash, gap
canvas.StrokeLineCap = LineCap.Round; // Butt, Round, Square
canvas.StrokeLineJoin = LineJoin.Round; // Miter, Round, Bevel
canvas.FillColor = Colors.CornflowerBlue;Shadows
canvas.SetShadow(
offset: new SizeF(5, 5),
blur: 4,
color: Colors.Gray);
canvas.FillRectangle(20, 20, 100, 60); // drawn with shadow
canvas.SetShadow(SizeF.Zero, 0, null); // remove shadowClipping
Restrict drawing to a region.
// Clip to rectangle
canvas.ClipRectangle(20, 20, 100, 100);
// Clip to arbitrary path
var clipPath = new PathF();
clipPath.AppendCircle(80, 80, 50);
canvas.ClipPath(clipPath);
// Subtract a region from the current clip
canvas.SubtractFromClip(40, 40, 30, 30);Quick Reference
| Method / Property | Purpose |
|---|---|
DrawLine | Stroke a line between two points |
DrawRectangle / FillRectangle | Stroke or fill a rectangle |
DrawRoundedRectangle / FillRoundedRectangle | Rounded-corner rectangle |
DrawEllipse / FillEllipse | Stroke or fill an ellipse |
DrawArc | Stroke an arc segment |
DrawPath / FillPath | Stroke or fill a PathF |
DrawString | Render text with alignment |
SetShadow | Apply drop shadow to subsequent draws |
ClipRectangle / ClipPath | Restrict drawing region |
SubtractFromClip | Remove area from clip |
SaveState / RestoreState | Push/pop canvas state |
graphicsView.Invalidate() | Request a redraw |