Ask Your Question

Revision history [back]

Have problem to show Mat image to picturebox VS2017

I want to show image in pictureBox. I already try to convert from mat to bitmap but it have error about pixelformat. I already check about my image type and channel. type of image is CV_8UC3.

Mat orig = imread("image_7.jpg");
    cvtColor(orig, orig, CV_BGR2RGB);
    System::Drawing::Graphics^ graphics = pictureBox1->CreateGraphics();
    System::IntPtr ptr(orig.ptr());
    System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(orig.cols, 
        orig.rows, orig.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
    System::Drawing::RectangleF rect(0, 0, pictureBox1->Width, pictureBox1->Height);
    graphics->DrawImage(b, rect);

error.

image description

but if I change code to

Mat orig = imread("image_7.jpg");
    cvtColor(orig, orig, CV_BGRA2RGBA);
    System::Drawing::Graphics^ graphics = pictureBox1->CreateGraphics();
    System::IntPtr ptr(orig.ptr());
    System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(orig.cols, 
        orig.rows, orig.step, System::Drawing::Imaging::PixelFormat::Format32bppArgb, ptr);
    System::Drawing::RectangleF rect(0, 0, pictureBox1->Width, pictureBox1->Height);
    graphics->DrawImage(b, rect);

It work but the color doesn't match original image. image description

original image.

image description