Skip to content

Instantly share code, notes, and snippets.

@MartyIX
Created July 25, 2024 08:49
Show Gist options
  • Save MartyIX/2513b1853ad8537a4d31b62c251115ad to your computer and use it in GitHub Desktop.
Save MartyIX/2513b1853ad8537a4d31b62c251115ad to your computer and use it in GitHub Desktop.
namespace Maui.Controls.Sample;
public class GraphicsDrawable(Microsoft.Maui.Graphics.IImage[] images) : IDrawable
{
public void Draw(ICanvas canvas, RectF dirtyRect)
{
// 3x3 matrix composed of 100x100 images. The number of images is always 9.
for (int i = 0; i < images.Length; i++)
{
int row = i / 3;
int column = i % 3;
canvas.DrawImage(images[i], column * 100, row * 100, 100, 100);
}
}
}
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.MainPage"
xmlns:local="clr-namespace:Maui.Controls.Sample">
<VerticalStackLayout>
<Button Text="Test" Clicked="OnCounterClicked"/>
<GraphicsView x:Name="graphicsVIew" WidthRequest="500" HeightRequest="500" />
</VerticalStackLayout>
</ContentPage>
using System.IO;
using Microsoft.Maui.Graphics.Platform;
namespace Maui.Controls.Sample;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void OnCounterClicked(object sender, EventArgs e)
{
try
{
using HttpClient httpClient = new();
int count = 9;
Stream[] streams = new Stream[count];
Task<Microsoft.Maui.Graphics.IImage>[] imageTasks = new Task<Microsoft.Maui.Graphics.IImage>[count];
// Download sample images.
for (int i = 0; i < count; i++)
{
streams[i] = await httpClient.GetStreamAsync("https://picsum.photos/100");
}
// Attempt to load them in parallel to make it more likely to hit any possible error.
for (int i = 0; i < count; i++)
{
Stream stream = streams[i];
imageTasks[i] = Task.Run(() => PlatformImage.FromStream(stream));
}
Microsoft.Maui.Graphics.IImage[] images = imageTasks.Select(task => task.Result).ToArray();
graphicsVIew.Drawable = new GraphicsDrawable(images);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment