Using Aspose.Drawing in Maui App
.NET Multi-platform App UI (.NET MAUI) is a cross-platform framework for creating native mobile and desktop apps with C# and XAML. Using .NET MAUI, you can develop apps that can run on Android, iOS, macOS, and Windows from a single shared code-base.
You can use Aspose.Drawing in your Maui app to draw vector graphics, text, and generate images as demonstrated in this tutorial.
1. Create a C# .NET MAUI App project.
In Visual Studio, create a new C# .NET MAUI App project, selecting .NET 9.0 without including sample content.
2. Add the Aspose.Drawing.Common package to the MauiApp1 project dependencies.
3. Add image drawing code.
Replace the MainPage.xaml.cs file with the following code that draws a gradient and creates an image:
// C# code to draw graphics and create image in Maui App. | |
// See https://docs.aspose.com/drawing/net/using-aspose-drawing-in-maui-app/ for complete details. | |
using Aspose.Drawing; | |
using Aspose.Drawing.Drawing2D; | |
using Aspose.Drawing.Imaging; | |
namespace MauiApp1 | |
{ | |
public partial class MainPage : ContentPage | |
{ | |
int count = 0; | |
public MainPage() | |
{ | |
InitializeComponent(); | |
SetLicense(); | |
AsposeImage.Source = ImageSource.FromStream(CreateImage); | |
} | |
private void OnCounterClicked(object sender, EventArgs e) | |
{ | |
count++; | |
if (count == 1) | |
CounterBtn.Text = $"Clicked {count} time"; | |
else | |
CounterBtn.Text = $"Clicked {count} times"; | |
SemanticScreenReader.Announce(CounterBtn.Text); | |
} | |
private Stream CreateImage() | |
{ | |
MemoryStream ms = new(); | |
Bitmap bitmap = new Bitmap(1000, 800, PixelFormat.Format32bppPArgb); | |
Graphics graphics = Graphics.FromImage(bitmap); | |
graphics.SmoothingMode = SmoothingMode.AntiAlias; | |
Aspose.Drawing.Brush brush = new Aspose.Drawing.Drawing2D.LinearGradientBrush(new Aspose.Drawing.Point(0, 0), new Aspose.Drawing.Point(1000, 800), Aspose.Drawing.Color.Red, Aspose.Drawing.Color.Blue); | |
graphics.FillEllipse(brush, 100, 100, 800, 600); | |
bitmap.Save(ms, Aspose.Drawing.Imaging.ImageFormat.Png); | |
ms.Seek(0, SeekOrigin.Begin); | |
return ms; | |
} | |
private void SetLicense() | |
{ | |
var stream = FileSystem.OpenAppPackageFileAsync("Aspose.Drawing.NET.lic").Result; | |
License license = new License(); | |
license.SetLicense(stream); | |
} | |
} | |
} |
4. Add an Aspose.Drawing license file.
Add your Aspose.Drawing.NET.lic file with Aspose.Drawing licensing information to the Resources\Raw folder in Solution Explorer.
5. Add UI image.
In the MainPage.xaml file, add the following code to the VerticalStackLayout control:
<Image x:Name=“AsposeImage” WidthRequest=“1000” HeightRequest=“800” />
6. Run the application.
Start the project from Visual Studio, the app will display the gradient image created with Aspose.Drawing: