Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
OpenCvSharp.Mat object has ToBitmap() method (to System.Drawing.Bitmap). I am using SkiaSharp library. How convert OpenCvSharp.Mat image to SKBitmap without using System.Drawing

What I have tried:

I tried to create my own method that converts OpenCvSharp.Mat to SKBitmap, but it didn't work
Posted

1 solution

Not my field of expertise at all but I found the following which might point you in the right direction -
System.Drawing.Image to SkiaSharp[^]

MS Learn | System.Drawing.Common only supported on Windows[^]

Convert System.Drawing.Icon to SkiaSharp.SKBitmap[^]

From what I could gather, you can use the byte array representation of the 'OpenCvSharp.Mat' image using 'ToBytes()' method and then create an 'SKBitmap' from the byte array using the 'SKBitmap.Decode' method. Your code should look something like the following based on the links given -

C#
using OpenCvSharp;
using SkiaSharp;

//Using your OpenCvSharp.Mat object 'mat'...
Mat mat = /* ... */

//Get the byte array representation of your Mat image...
byte[] bytes = new byte[0];
try
{
    bytes = mat.ToBytes(".png");
}
catch (Exception ex)
{
    //Handle your exception here...
}

//Create an SKBitmap from the returned byte array...
SKBitmap bitmap = SKBitmap.Decode(bytes);


You might want to handle the exception accordingly if the conversion fails.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900