【问题标题】:Error C2440 : cannot cast 'System::Drawing::Bitmap' into 'System::Object'错误 C2440:无法将“System::Drawing::Bitmap”转换为“System::Object”
【发布时间】:2017-06-22 08:17:56
【问题描述】:

我最近开始使用 OpenCV,努力将基于它的插件集成到现有项目中。 这个使用 System::Drawing::Bitmap 来管理图像和一个 System::EventArgs 派生类(不确定那个词),其中包含一个用于在插件之间传输数据的 System::Object 元素。

OpenCV 使用 C++ 我必须对我的插件进行相应的编程,但感谢 CLR(不太确定那个)我的新 c++ 类可以继承我的 C#“插件”接口。

这个插件非常简单,将 cv::Mat 转换为位图,然后将其转换为先前引用的数据对象并调用事件。

但它给了我错误 C2440 cannot cast from System::Drawing::Bitmap to System::Object。

我在 C# 中将位图转换为对象没有任何问题,但现在我在 C++ 中,这不再起作用了。

这怎么可能?我的意思是 .Net 的重点是无论我使用 C# 还是 C++,Bitmap 的继承都是相同的吗?

也许我没有完全掌握 clr 的东西以及它是如何工作的。 无论如何,提前感谢您的帮助。

.h 文件:

namespace PluginCV 
{
    public ref class Mat2Bitmap : public InterfacePlugin::IPlugin
    {
    private:
        bool _isReady;
        System::EventHandler^ evt;
        System::Drawing::Bitmap^ Convert(cv::Mat img);
        cv::Mat* img;

    public:
        virtual void __clrcall Start(void) sealed;
        virtual void __clrcall Stop(void) sealed;
        virtual void __clrcall Handle(Object ^ obj, EventArgs ^ args) sealed;
        virtual void __clrcall InitWithNetwork(Object^ obj, int port) sealed;

        virtual property EventHandler^ DoneEvent
        {
            void set (EventHandler^ e) sealed { evt = e; };
            EventHandler^ get(void) sealed { return evt; };
        }

        virtual property bool isReady
        {
            void set (bool b) sealed { _isReady = b; };
            bool get(void) sealed { return _isReady; };
        }
    };
}

.cpp:

#include "Stdafx.h"

//#include "PluginCV.h"
#include "MatEventArg.h"

void PluginCV::Mat2Bitmap::Stop(void)
{

}

 void PluginCV::Mat2Bitmap::Start(void) 
{
     System::Drawing::Bitmap Bmp = Convert(*img);

     DATAMODEL::BBEventArgs bb;
     bb.Data = (System::Object)Bmp;
}

 System::Drawing::Bitmap^ PluginCV::Mat2Bitmap::Convert(cv::Mat img)
 {
     cv::Size s = img.size();
     System::Drawing::Bitmap^ bmp = gcnew System::Drawing::Bitmap(s.width, s.height, img.step1(), System::Drawing::Imaging::PixelFormat::Format24bppRgb, (System::IntPtr)img.data);

     return bmp;
 }

void PluginCV::Mat2Bitmap::Handle(Object^ obj, EventArgs^ args)
{
    PluginCV::MatEventArg^ e = (PluginCV::MatEventArg^)args;

    e->img->copyTo(*img);

    Start();
}

void PluginCV::Mat2Bitmap::InitWithNetwork(Object^ obj, int port)
{

}

错误代码(法语):

Erreur  1   error C2440: 'cast de type' : impossible de convertir de 'System::Drawing::Bitmap' en 'System::Object'  C:\SIMON\PluginCV\PluginCV\Mat2Bitmap.cpp   16  1   PluginCV

【问题讨论】:

  • 错误指向哪一行?
  • cpp 文件中位图到对象的转换行 (16):bb.Data = (System::Object)Bmp;

标签: c++ .net opencv casting bitmap


【解决方案1】:

PC 重启就成功了。 由于 Bitmap 继承了 Object ,所以不知道是什么导致它无法工作......

不管怎样,再见。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    相关资源
    最近更新 更多