Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
.NET Multi-platform App UI (.NET MAUI) provides a cross-platform graphics canvas on which 2D graphics can be drawn using types from the Microsoft.Maui.Graphics namespace. This canvas supports drawing and painting shapes and images, compositing operations, and graphical object transforms.
There are many similarities between the functionality provided by Microsoft.Maui.Graphics, and the functionality provided by .NET MAUI shapes and brushes. However, each is aimed at different scenarios:
For more information about .NET MAUI shapes, see Shapes.
In .NET MAUI, the GraphicsView enables consumption of Microsoft.Maui.Graphics functionality. GraphicsView defines the Drawable
property, of type IDrawable
, which specifies the content that will be drawn by the control. To specify the content that will be drawn you must create an object that derives from IDrawable
, and implement its Draw
method:
namespace MyMauiApp
{
public class GraphicsDrawable : IDrawable
{
public void Draw(ICanvas canvas, RectF dirtyRect)
{
// Drawing code goes here
}
}
}
The Draw
method has ICanvas and RectF
arguments. The ICanvas argument is the drawing canvas on which you draw graphical objects. The RectF
argument is a struct
that contains data about the size and location of the drawing canvas.
In XAML, the IDrawable
object can be declared as a resource and then consumed by a GraphicsView by specifying its key as the value of the Drawable
property:
<ContentPage xmlns=http://schemas.microsoft.com/dotnet/2021/maui
xmlns:x=http://schemas.microsoft.com/winfx/2009/xaml
xmlns:drawable="clr-namespace:MyMauiApp"
x:Class="MyMauiApp.MainPage">
<ContentPage.Resources>
<drawable:GraphicsDrawable x:Key="drawable" />
</ContentPage.Resources>
<VerticalStackLayout>
<GraphicsView Drawable="{StaticResource drawable}"
HeightRequest="300"
WidthRequest="400" />
</VerticalStackLayout>
</ContentPage>
For more information about the GraphicsView, see GraphicsView.
The GraphicsView control provides access to an ICanvas object, via its IDrawable
object, on which properties can be set and methods invoked to draw graphical objects. For information about drawing on an ICanvas, see Draw graphical objects.
ICanvas defines the following properties that affect the appearance of objects that are drawn on the canvas:
float
, indicates the opacity of an object.bool
, specifies whether anti-aliasing is enabled.float
, represents the scaling factor to scale the UI by on a canvas.float
, defines the size of the font when drawing text.float
, specifies the limit of the miter length of line joins in an object.float
, specifies the distance within the dash pattern where a dash begins.float[]
, specifies the pattern of dashes and gaps that are used to outline an object.float
, indicates the width of an object's outline.By default, an ICanvas sets StrokeSize to 1, StrokeColor to black, StrokeLineJoin to LineJoin.Miter
, and StrokeLineCap to LineJoin.Cap
.
The drawing canvas on each platform has the ability to maintain its state. This enables you to persist the current graphics state, and restore it when required.
However, not all elements of the canvas are elements of the graphics state. The graphics state does not include drawing objects, such as paths, and paint objects, such as gradients. Typical elements of the graphics state on each platform include stroke and fill data, and font data.
The graphics state of each ICanvas can be manipulated with the following methods:
Note
The state that's persisted by these methods is platform dependent.
.NET MAUI feedback
.NET MAUI is an open source project. Select a link to provide feedback:
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Create a UI in a .NET MAUI app by using XAML - Training
Learn how to design a UI for a .NET MAUI app using XAML.
Documentation
.NET MAUI graphics includes functionality to load, save, resize, and downsize images.
.NET MAUI - GraphicsView - Code Samples
This sample demonstrates how to use the .NET MAUI GraphicsView.
Draw graphical objects - .NET MAUI
.NET MAUI graphics enables you to draw graphical objects on a canvas.