question

yusuf2 avatar image
0 Votes"
yusuf2 asked LeonLu-MSFT answered

Save an image using Maui.net graphics

How do I save Maui.net graphics to png file ?

dotnet-csharpdotnet-maui
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
1 Vote"
LeonLu-MSFT answered

Hello,​


You can install Microsoft.Maui.Graphics.Skia in the nuget package manager, then use SkiaBitmapExportContext to create a bitmap in memory and draw on its Canvas

using Microsoft.Maui.Graphics;
using Microsoft.Maui.Graphics.Skia;

 SkiaBitmapExportContext skiaBitmapExportContext= new(width, height, displayScale);
 ICanvas canvas = skiaBitmapExportContext.Canvas;


After drawing, you can save image as png file with WriteToFile method, I save this image at FileSystem.Current.AppDataDirectory path.

string mainDir = FileSystem.Current.AppDataDirectory;
        string filePath = Path.Combine(mainDir, "MyTest.png");
        // Save the image as a PNG file
        skiaBitmapExportContext.WriteToFile(filePath);




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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.