问题
I want to convert an openCV Mat file into a GDI+ BitMap image. I cannot find any information on how to do this?
I think there is no straight way of doing this, but I hope that it is not involved of writing it to a file and reading back (http://opencv-users.1802565.n2.nabble.com/Convert-IplImage-to-Bitmap-td3784378.html )
I need to write something such as this:
#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;
Bitmap GetBitMap(cv::Mat inputImage)
{
Bitmap bitmap;
// convert inputImage to bitmap??
return bitmap;
}
回答1:
According to the documentation, it appears that Bitmap
has a constructor taking a pre-allocated memory buffer. Using this constructor, your function might look like this:
Bitmap GetBitMap(cv::Mat inputImage)
{
cv::Size size = inputImage.size();
Bitmap bitmap(size.width, size.height, inputImage.step1(), PixelFormat24bppRGB, inputImage.data);
return bitmap;
}
Be advised that the Bitmap
returned from your function does not manage the memory you initialize it with. Thus, make sure the image data is being kept alive for the lifetime of the Bitmap
.
来源:https://stackoverflow.com/questions/19568158/how-to-convert-an-opencv-mat-image-to-gdi-bitmap