Save an image using Maui.net graphics

Yusuf 791 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.
3,482 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,908 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 75,351 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. Fabio Iko Motta 5 Reputation points
    2024-07-06T13:23:00.6833333+00:00

    //HOW TO DRAW ON AN IMAGE USING MICROSOFT.MAUI.GRAPHICS AND PASTE OVER AN IMAGE USING SKIASHARP

    //load an image

    IImage image = await LoadImageAsync();

    // Create SkiaBitmapExportContext with transparence

    using (var skiaBitmapExportContext = new SkiaBitmapExportContext((int)image.Width, (int)image.Height, 1, 72, true, true))

    {

    //Create a SKCanvas
    
    using var bitmap = new SKBitmap((int)image.Width, (int)image.Height);
    
    using var skCanvas = new SKCanvas(bitmap);
    
    //Draw an image
    
    using var stream0= new MemoryStream(image.AsBytes());
    
    using var skImage = SKImage.FromEncodedData(stream0);
    
    stream0.Position = 0;
    
    skCanvas.DrawImage(skImage, new SKPoint(0, 0));
    
    //Get a Microsoft.Maui.Graphics.ICanvas used by Maui to draw
    
    ICanvas canvas = skiaBitmapExportContext.Canvas;
    
    // Draw a red line using Maui 
    
    canvas.StrokeColor = Colors.Red;
    
    canvas.StrokeSize = 8;
    
    canvas.DrawLine(new Microsoft.Maui.Graphics.PointF(100, 100), new Microsoft.Maui.Graphics.PointF(500, 500));
    
    canvas.SaveState();
    
    //Create a stream to save the draw on ICanvas to SKCanvas
    
    using var stream1= new MemoryStream();
    
    skiaBitmapExportContext.WriteToStream(stream1);
    
    stream1.Position = 0; // Reset position to start
    
    //Send draw as image with transparence to SKCanvas applying over image sent before
    
    using var skImageApliques = SKImage.FromEncodedData(stream1);
    
    skCanvas.DrawImage(skImageApliques, new SKPoint(0, 0));
    
    //Upload to Azure Blob Storage or wherever you want
    
    var stream2 = new MemoryStream();
    
    bitmap.Encode(stream2, SKEncodedImageFormat.Jpeg, 100);
    
    stream2.Position = 0; // Reset position to start
    
    await UploadPDFAsMemoryStream(blobClient, "ImagemWithDraw.jpg", stream2);
    
    //Maybe just one stream be enough
    

    }

    1 person found this answer helpful.
    0 comments No comments

  2. 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

  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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.