Save an image using Maui.net graphics

Yusuf 691 Reputation points
2022-06-23T18:14:21.013+00:00

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

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,897 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,274 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    2022-06-24T06:10:44.987+00:00

    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.

    2 people found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Matt Lovell 0 Reputation points
    2023-06-07T19:04:11.6333333+00:00

    Hi, I'm just getting a blank screen when I implement the above - but with no errors.

    My project is using net7

    Target is Android API 33

    Maui Graphics 7.0.81

    Microsoft.Maui.Graphics.Skia 7.0.81

    Any ideas as to why this may not be working?

    Or should I be using PlatformCanvas?

    Thanks

    matt

    0 comments No comments

  2. Yusuf 691 Reputation points
    2023-09-10T13:15:08.7666667+00:00

    Is there anyone who knows how to save a graphic? I haven't been able to find a solution, and the provided answer didn't resolve the issue. Any help would be greatly appreciated.

    0 comments No comments

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more