Images in .NET MAUI
Images are a fundamental part of modern application development, enhancing user experience and conveying information visually. .NET MAUI (Multi-platform App UI) provides robust support for displaying images in various ways, allowing you to load images from different sources, control their appearance, and integrate them seamlessly into your UI.
Displaying Images
The primary control for displaying images in .NET MAUI is the
control. It supports loading images from several sources, including embedded resources, local file paths, and URIs.Image
Loading from Embedded Resources
Embedding images directly into your project is the recommended approach for platform-agnostic image handling. These images are compiled into your application assembly.
To embed an image:
- Add the image file to your project (e.g., in an
Resources/Images
folder). - Right-click the image file in the Solution Explorer, select Properties, and set the Build Action to MauiImage.
You can then reference the image using its filename:
<Image Source="my_logo.png" />
For images in subfolders within Resources/Images
, use a forward slash:
<Image Source="Images/Icons/app_icon.png" />
Loading from Local Files
You can also load images from the device's file system. This is useful for images captured by the camera or downloaded by the user.
<Image Source="{Binding ImagePath}" />
In your C# code-behind or ViewModel, ensure ImagePath
is a string representing the file path.
Loading from URIs
Remote images can be loaded directly from web URLs. .NET MAUI handles asynchronous downloading and caching.
<Image Source="https://docs.microsoft.com/dotnet/maui/media/logo.png" />
The
control automatically caches downloaded images for better performance.Image
Image Properties
The
control offers several properties to customize its appearance and behavior:Image
: Controls how the image is scaled to fit its container. Common values includeAspect
(scales image to fit while preserving aspect ratio) andAspectFit
(scales image to fill the entire container while preserving aspect ratio).AspectFill
andHeightRequest
: Specify explicit dimensions for the image.WidthRequest
: Controls whether animated images (like GIFs) are playing.IsAnimationPlaying
: Useful for UI testing.AutomationId
Controlling Aspect Ratio
Let's see how
and AspectFit
affect image display:AspectFill
<Image Source="sample_image.jpg" Aspect="AspectFill" HeightRequest="200" WidthRequest="300" /> <Image Source="sample_image.jpg" Aspect="AspectFit" HeightRequest="200" WidthRequest="300" />
Handling Animation
For animated image formats like GIFs, you can control playback:
<Image Source="loading_animation.gif" IsAnimationPlaying="{Binding IsLoading}" />
Vector Graphics (SVG)
For scalable vector graphics, .NET MAUI leverages the SkiaSharp graphics library. You can display SVG files by using the
package.CommunityToolkit.Maui.Graphics
First, install the NuGet package:
dotnet add package CommunityToolkit.Maui.Graphics
Then, in your
, register the graphics services:MauiProgram.cs
builder.UseMauiApp<App>().ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); }).UseMauiCommunityToolkitGraphics(); // Add this line
You can then display SVGs using the
control, often by defining a custom GraphicsView
:IGraphicsDrawable
<GraphicsView x:Name="MyGraphicsView" />
And in code-behind:
public partial class MyPage : ContentPage { public MyPage() { InitializeComponent(); MyGraphicsView.Drawable = new SvgDrawable("Resources/Images/vector_icon.svg"); } }
The
class would handle parsing and drawing the SVG content.SvgDrawable
Image Caching
When loading images from URIs, .NET MAUI provides automatic caching to improve performance. The cache is based on the URI. If you need to disable caching or manage it manually, you can explore more advanced options, but for most cases, the default caching is sufficient.
To clear the cache manually, you can use:
Microsoft.Maui.Controls.Image.ClearCache(new Uri(imageUrl));