How to convert an opencv mat image to gdi bitmap

余生长醉 提交于 2020-01-03 06:22:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!