Windows 形式的 C++ rtsp 流

C++ rtsp stream in Windows Forms

我有一个程序可以从相机接收 rtsp 视频并将其传输到图片框。只运行了几秒,然后程序就挂了,帮忙找找错误。

private: System::Void start_Click(System::Object^ sender, System::EventArgs^ e) {
if (start->Text == "Start stream"){
    start->Text = "Stop stream";
}
else if (start->Text == "Stop stream") {
    pictureBox1->Refresh();
    start->Text = "Start stream";
}
cv::VideoCapture stream = cv::VideoCapture(ip[n]);
cv::Mat frame;
while (start->Text == "Stop stream")
{
    stream.read(frame);
    System::Drawing::Graphics^ graphics = pictureBox1->CreateGraphics();
    System::IntPtr ptr(frame.ptr());
    System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(frame.cols, frame.rows, frame.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
    System::Drawing::RectangleF rect(0, 0, pictureBox1->Width, pictureBox1->Height);
    graphics->DrawImage(b, rect);
    delete graphics;    
}

已通过更改 while(表达式)

修复
cv::VideoCapture stream = cv::VideoCapture(ip[n]);
        cv::Mat frame;
        if (stream.isOpened())
        {
            flag = true;
            while (cv::waitKeyEx(1) != 27)
            {
                stream.read(frame);
                System::Drawing::Graphics^ graphics = pictureBox1->CreateGraphics();
                System::IntPtr ptr(frame.ptr());
                System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(frame.cols, frame.rows, frame.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
                System::Drawing::RectangleF rect(0, 0, pictureBox1->Width, pictureBox1->Height);
                graphics->DrawImage(b, rect);
                delete graphics;
            }
        }