Diogo Cardoso
Diogo Cardoso

Reputation: 23

Converting System::Drawing::Bitmap^ to ::MAT

How converting image from picturebox to ::MAT in C++/CLI?

Thank you

Upvotes: 1

Views: 2722

Answers (1)

Yochai Timmer
Yochai Timmer

Reputation: 49221

You need to cast the Drawing.Image into a Bitmap (assuming that the image REALLY IS a bitmap).
Then lock the System.Drawing.Bitmap, and use the Scan0 property of the BitmapData to access the inner buffer.

System::Drawing::Bitmap ^ bitmapFrame = safe_cast< System::Drawing::Bitmap ^ >(pictureBox1->Image);

BitmapData^ bmpData = bitmapFrame->LockBits(gcnew Rectangle(0, 0, bitmapFrame->Width, bitmapFrame->Height), System::Drawing::Imaging::ImageLockMode::ReadWrite, 
            bitmapFrame->Format);
try
{    
    void* data = bmpData.Scan0;

    //use the data in the ::Mat constructor.
}
finally { bitmapFrame->UnlockBits(bmpData); }//Remember to unlock!!!

Upvotes: 1

Related Questions