How to convert an opencv mat image to gdi bitmap

15,828

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.

Share:
15,828
mans
Author by

mans

Updated on June 04, 2022

Comments

  • mans
    mans almost 2 years

    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;
    }
    
  • mans
    mans over 10 years
    I am not sure that it works when OpenCv image is not continues?
  • mrid
    mrid over 4 years
    @mans what do mean by the bitmap being continuous? can you please explain