First, I tried this,
public static Bitmap MatToBitmap(Mat mat)
{
return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
}
So, then, I tried this,
public static Bitmap MatToBitmap(Mat mat)
{
mat.ConvertTo(mat, MatType.CV_8U);
return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
}
The image is completely black,
public static Bitmap ConvertMatToBitmap(Mat matToConvert)
{
return new Bitmap(matToConvert.Cols, matToConvert.Rows, 4*matToConvert.Rows, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, matToConvert.Data);
}
This doesn't work either.
The others solutions almost worked for me.
The bitmaps were there, but sometimes an exception saying that the encoder
was null
was threw.
I solved using the following code.
private static Bitmap MatToBitmap(Mat mat) {
using (var ms = mat.ToMemoryStream()) {
return (Bitmap) Image.FromStream(ms);
}
}
mat have a method that return Bitmap:
mat.ToBitmap(bitmap);
"using OpenCvSharp.Extensions;" is also needed for this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With