Showing Mat image in Picturebox
Hi guys,
I'm attempting to create a GUI for my project and I am having issues displaying the output in the GUI picturebox. I used the following function I found online to convert the Mat image to bitmap:
void DrawCVImage(System::Windows::Forms::Control^ control, cv::Mat& colorImage)
{
System::Drawing::Graphics^ graphics = control->CreateGraphics();
System::IntPtr ptr(colorImage.ptr());
System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(colorImage.cols, colorImage.rows, colorImage.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
System::Drawing::RectangleF rect(0, 0, control->Width, control->Height);
graphics->DrawImage(b, rect);
}
I then just did a simple:
frame = imread("C:\\Users\\xxx\\Pictures\\picture.jpg");
DrawCVImage(pictureBox1,frame);
but only a tiny bit of the picture appears as shown here: http://i.imgur.com/KKviOOv.jpg
Thanks in advance!
System::Drawing::Imaging::PixelFormat::Format24bppRgb
Basically this is your problem. I am guessing that this is NOT the format of your frame. A frame read like that is only 8 bit 3 channel data. More than half of your image data is skipped due to taking larger step sizes.
Hi Steven! Thanks for helping me out with this.
Is there a way to fix this? I'm new to C++/CLI coding and Windows Forms. I read that Mat images can only be converted to bitmap if they are in CV_8UC3 format so I tried doing a frame.convertTo(frameDisplay, CV_8UC3). It worked for 2 images but then suddenly stopped working all together.
I apologize if I'm missing what you're trying to say completely. I'm new to C++, OpenCV and especially Windows Forms. I've done all my C++/OpenCV codes. Now I'm just left with making everything more visually appealing with a proper GUI.
Once again, thanks for your help!