Lost in the Transition to .NET MAUI and Maui Graphics

Yusuf 681 Reputation points
2023-09-14T03:34:39.9666667+00:00

Hello everyone,

I am writing to seek help with creating a graphics using .NET MAUI with Maui Graphics or SkiaSharp. I have recently migrated from Xamarin Forms to .NET MAUI and from SkiaSharp to Maui Graphics. Unfortunately, I have lost some of my previous knowledge and have not yet learned the new techniques as there are no available instructions for Maui Graphics as there were for SkiaSharp.

I would appreciate any guidance or resources that can help me learn how to create 2D graphics using Maui Graphics. Additionally, I would like to know how to create and save graphical content on Android devices using any technology.

Thank you in advance!

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,870 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    2023-09-15T03:34:51.8133333+00:00

    Hello,

    I would appreciate any guidance or resources that can help me learn how to create 2D graphics using Maui Graphics.

    Before you start to draw graphics, please read this Graphics document,

    Then you can try to draw a 2D grahics by Drawing graphical objects

    Additionally, I would like to know how to create and save graphical content on Android devices using any technology.

    If you want to save the graphical content, you can use File system helpers and GraphicsView.CaptureAsync() method

    Firstly, if you have a GraphicsView like following code.

    <GraphicsView
          x:Name="myGraphicsView"
                    Drawable="{StaticResource drawable}"
                    HeightRequest="300"
                    WidthRequest="400" />
    

    Then, you can save it from backgroud code. 1. get the IScreenshotResult by myGraphicsView.CaptureAsync(), 2. Create an output file, copy the 2D graphics to this output file by await screenshotResult.CopyToAsync(outputStream);.

    IScreenshotResult screenshotResult= await myGraphicsView.CaptureAsync();
    
    if (screenshotResult!=null)
    {
        // Create an output filename
         targetFile = Path.Combine(FileSystem.Current.AppDataDirectory, "test2.png");         
        // Copy the file to the AppDataDirectory
        using FileStream outputStream = File.Create(targetFile);
        if(File.Exists(targetFile))
        {
            await screenshotResult.CopyToAsync(outputStream);
        }  
    }
    

    By the way, if you want to get this 2D graphics and display it to Image control, you can do this by following code.

    if(File.Exists(targetFile)) {
        FileStream fileStream = File.Open(targetFile, FileMode.Open);
        MydrawImage.Source = ImageSource.FromStream(() => fileStream);
    }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.