MAUI GraphicsView canvas save to file

Caner AKAR 20 Reputation points
2024-01-05T06:50:46.02+00:00

Hello;

I have a problem that I cannot solve;

I want to save the image created in Maui GraphicsView.

But I failed. Do you have any knowledge about this?

Thanks.

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

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 71,596 Reputation points Microsoft Vendor
    2024-01-08T01:17:06.86+00:00

    Hello,

    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 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);
        }  
    }
    

    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Caner AKAR 20 Reputation points
    2024-01-28T05:04:30.7366667+00:00

    Thanks. Good solution.

    0 comments No comments