Dealing with Images - Very new to C# and MAUI

KSD 5 Reputation points
2023-05-23T10:59:20.52+00:00

Hi All,

I'm really new to this, and am trying to follow Youtube and Skillshare tutorials, but there are gaps in what's presented when it comes to dealing with images and with just the code provided in API's, I'm struggling to achieve what should be a simple task.

I'm trying to follow the info here to manipulate images: https://learn.microsoft.com/en-us/dotnet/maui/user-interface/graphics/images but there is a gap in my knowledge on how to actually implement the code samples provided.

using Microsoft.Maui.Graphics.Platform;
...

IImage image;
Assembly assembly = GetType().GetTypeInfo().Assembly;
using (Stream stream = assembly.GetManifestResourceStream("GraphicsViewDemos.Resources.Images.dotnet_bot.png"))
{
    image = PlatformImage.FromStream(stream);
}

// Save image to a memory stream
if (image != null)
{
    IImage newImage = image.Downsize(150, true);
    using (MemoryStream memStream = new MemoryStream())
    {
        newImage.Save(memStream);
    }
}

I have my test code at a point where I can take a photo (Android) and have it save to the gallery, but I want to use Maui.Graphics to Downsize the image before it is saved.

This code snippet is where the photo is taken and code below it saves it to the gallery, how do I use the sample code in the API docs above to resize the photo before the code continues on to save the file?

    async void Button_Clicked(object sender, EventArgs e)
    {
		var result = await MediaPicker.CapturePhotoAsync();

		if (result != null)
		{
			using var stream = await result.OpenReadAsync();

			using var memoryStream = new MemoryStream();

            stream.CopyTo(memoryStream);

            stream.Position = 0;   
            memoryStream.Position = 0;

Thanks in advance.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,186 questions
{count} votes