OpenCV cv::Mat to System::Drawing::位图抛出System.ArgumentException(OpenCV C+;+;/CLI,x64)

OpenCV cv::Mat to System::Drawing::位图抛出System.ArgumentException(OpenCV C+;+;/CLI,x64),opencv,c++-cli,visual-studio-2019,Opencv,C++ Cli,Visual Studio 2019,我试图在c++/CLR项目中将cv::Mat转换为System::Drawing::Bitmap^。 我已经看过很多这样做的例子,但是我的程序只在运行时点击按钮后抛出一个系统.ArgumentException private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { cv::Mat img = cv::imread("C://picture//col

我试图在c++/CLR项目中将cv::Mat转换为System::Drawing::Bitmap^。 我已经看过很多这样做的例子,但是我的程序只在运行时点击按钮后抛出一个系统.ArgumentException

    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

        cv::Mat img = cv::imread("C://picture//colors.jpg", cv::IMREAD_COLOR);

        // OK
        cv::imshow("Colors", img);
        // OK
        System::Drawing::Bitmap^ a = gcnew System::Drawing::Bitmap(img.cols, img.rows, System::Drawing::Imaging::PixelFormat::Format24bppRgb);

        System::IntPtr ptr = (System::IntPtr)img.data;

        // KO
        System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(img.cols, img.rows, img.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
        pictureBox1->Image = b;
    }
例外情况:

System.ArgumentException: 'invalid parameter'.
This exception was originally thrown at this call stack:
    System.Drawing.Bitmap.Bitmap(int, int, int, System.Drawing.Imaging.PixelFormat, System.IntPtr)
    FromMatToBitmap::FromMToB::button1_Click(System::Object^, System::EventArgs^) in FromMToB.h
    System.Windows.Forms.Control.OnClick(System.EventArgs)
    System.Windows.Forms.Button.OnClick(System.EventArgs)
    System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs)
    System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message, System.Windows.Forms.MouseButtons, int)
    System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message)
    System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message)
    System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message)
    System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr, int, System.IntPtr, System.IntPtr)
    ...
    [Call Stack Truncated]
问题似乎出在系统::IntPtr ptr上,但编译器没有显示任何错误

C++/CLI属性: 公共语言运行时支持:“公共语言运行时支持(/clr)”, .NET目标框架版本:“v4.7.2”, 启用托管增量生成:“是”


OpenCV版本:4.5.0

这是否回答了您的问题?这回答了你的问题吗?