在Windows窗体C++;Visual Studio 2019 我对C++的GUI设计是新手,对指针的使用还不太熟悉。最近,我在尝试在构建的gui中的PictureBox中显示OpenCV Mat图像时遇到了一些问题。我在网上搜索过,甚至找到了一篇与我的问题非常相似的帖子,但是当我试图实现指南时,我在尝试操作gui时遇到了一个异常

在Windows窗体C++;Visual Studio 2019 我对C++的GUI设计是新手,对指针的使用还不太熟悉。最近,我在尝试在构建的gui中的PictureBox中显示OpenCV Mat图像时遇到了一些问题。我在网上搜索过,甚至找到了一篇与我的问题非常相似的帖子,但是当我试图实现指南时,我在尝试操作gui时遇到了一个异常,c++,opencv,user-interface,C++,Opencv,User Interface,非常相似的职位 我从这篇文章中抓取的代码为您保存了一次点击: void DrawCVImage(System::Windows::Forms::Control^ control, cv::Mat& colorImage) { System::Drawing::Graphics^ graphics = control->CreateGraphics(); System::IntPtr ptr(colorImage.ptr()); System::Drawin

非常相似的职位

我从这篇文章中抓取的代码为您保存了一次点击:

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);
    delete graphics;
}
现在我试图显示一个“视频”提要(实际上是一个
cv::Mat
对象的数组),但我有一个
“源不可用”/System.ArgumentException:'参数无效。
当我试图调用容纳该回放的函数时,屏幕出现。我还知道引发问题的特定代码行是

System::Drawing::Bitmap^ b  = gcnew System::Drawing::Bitmap(colorImage.cols,colorImage.rows,colorImage.step,System::Drawing::Imaging::PixelFormat::Format24bppRgb,ptr);
现在,关于具体内容,表单有一个事件(当前是单击图片框,但我希望将来移动到“播放”按钮),在这个事件上,代码:

private: System::Void leftEyeImage_Click(System::Object^ sender, System::EventArgs^ e) {
    std::cout << "Click Received" << std::endl;
    cv::Mat& frame = cv::imread("Desktop/testcapture.png");
    std::cout << "Import successful" << std::endl; 
    drawLeftEye(this, frame);
}
我不太确定如何解决这个问题,我的朋友提到这可能是一个记忆问题,但我不知道我将如何着手解决它。这也很可能是我在设计这个或我的指针要去的地方犯了一个简单的错误,我只是没有足够的能力知道错误

顺便说一下,函数中调用的“this”指针链接到Windows自动生成的代码(来自gui构造):


任何和所有的建议或测试将非常感谢,因为我对答案和推理都感兴趣。提前谢谢

您好,在图片框中显示垫子时,我遇到了类似的问题。如果您发现了任何解决方案,请与我们分享。谢谢这回答了你的问题吗?
namespace DevProject {
    void ShowResults::drawLeftEye(System::Windows::Forms::Control^ control, cv::Mat& framebmp) {
        System::Drawing::Graphics^ graphics = control->CreateGraphics();
        std::cout << "Now Here" << std::endl;
        System::IntPtr ptr(framebmp.data);  

        // Issue line 
        System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(framebmp.cols, framebmp.rows, framebmp.step, System::Drawing::Imaging::PixelFormat::Format32bppRgb, ptr);

        std::cout << "Converted successfully" << std::endl;

        System::Drawing::RectangleF rect(0, 0, control->Width, control->Height);        //No issue
        graphics->DrawImage(b, rect);                                   
        std::cout << "Now Here before delete" << std::endl;
        delete graphics;
        //delete b;
    }
}
System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(framebmp.cols, framebmp.rows, framebmp.step, System::Drawing::Imaging::PixelFormat::Format32bppRgb, ptr);

//As well as (but only because b is defined in the line above^
graphics->DrawImage(b, rect); 
this->leftEyeImage->Anchor = System::Windows::Forms::AnchorStyles::None;
this->leftEyeImage->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
this->leftEyeImage->Location = System::Drawing::Point(11, 836);
this->leftEyeImage->Name = L"leftEyeImage";
this->leftEyeImage->Size = System::Drawing::Size(991, 646);
this->leftEyeImage->TabIndex = 4;
this->leftEyeImage->TabStop = false;
this->leftEyeImage->Click += gcnew System::EventHandler(this, &ShowResults::leftEyeImage_Click);