5

我需要使用 openFileDialog 读取 Mat 形式的图像并将其显示在图片框中(在 Visual C++/Visual Studio 2010 中)。

我搜索了很多,但找不到答案。

我正在使用这段代码:

openFileDialog1->Filter = "JPEG files (*.jpg)|*.jpg|Bitmap files (*.bmp)|*.bmp";
if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
    Mat img;
    img = imread(openFileDialog1->FileName, CV_LOAD_IMAGE_COLOR);
    pictureBox1->Image = (gcnew Bitmap(img.size().width,
                                            img.size().height,
                                            img.widthStep,
                                            Imaging::PixelFormat::Format24bppRgb,
                                            (IntPtr)img.data));

}
4

2 回答 2

2

这个问题已经在这里得到了回答:

根据您的要求,您可以这样做:

Mat img;
img = imread(openFileDialog1->FileName, CV_LOAD_IMAGE_COLOR);

System::Drawing::Graphics^ graphics = pictureBox1->CreateGraphics();
System::IntPtr ptr(img.ptr());
System::Drawing::Bitmap^ b  = gcnew System::Drawing::Bitmap(img.cols,img.rows,img.step,System::Drawing::Imaging::PixelFormat::Format24bppRgb,ptr);
System::Drawing::RectangleF rect(0,0,pictureBox1->Width,pictureBox1->Height);
graphics->DrawImage(b,rect);
于 2013-05-03T19:41:14.900 回答
1

您需要像这样设置 Picturebox 的调色板:

ColorPalette^ palette = pictureBox1->Image->Palette;
UInt32 Alpha = 0xFF;
UInt32 Intensity;

for (System::UInt16 i = 0; i < palette->Entries->Length; ++i)
{

    Intensity = i * 0xFF / 255;

    palette->Entries[i] = Color::FromArgb(static_cast<int>(Alpha),
                                          static_cast<int>(Intensity),
                                          static_cast<int>(Intensity),
                                          static_cast<int>(Intensity));
}

pictureBox1->Image->Palette = palette;
于 2015-09-23T08:12:51.603 回答