错误 C2440:无法将"系统::D rawing::位图"转换为"系统::对象"

Error C2440 : cannot cast 'System::Drawing::Bitmap' into 'System::Object'

本文关键字:系统 位图 转换 对象 rawing 错误 C2440      更新时间:2023-10-16

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

OpenCV 在C++我必须相应地编程我的插件,但多亏了 CLR(不太确定),我的新 c++ 类可以继承我的 C# "插件"接口。

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

但它给了我错误 C2440 无法从系统::D rawing::位图转换为系统::对象。

我在 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:SIMONPluginCVPluginCVMat2Bitmap.cpp   16  1   PluginCV

PC 重新启动可以解决问题。 不知道是什么原因导致这不起作用,因为位图继承了对象......

无论如何,告别。