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

老子叫甜甜 提交于 2019-12-13 03:54:57

问题


I recently started tu use OpenCV in an effort to integrate plugins based on it to an already existing project. This one uses System::Drawing::Bitmap to manage images and a System::EventArgs derivative (not sure about that word) containing a System::Object element used to transfer data between plugins.

OpenCV beeing in C++ I have to program my plugin accordingly but thanks to CLR (not very sure about that one) my new c++ class can inherit my C# "Plugin" interface.

This one plugin is pretty simple, converting cv::Mat to a Bitmap then casting it into the Data Object previously cited and invoking an event.

But it's giving me error C2440 cannot cast from System::Drawing::Bitmap to System::Object.

I didnt have any problem casting a Bitmap to an Object in C# but now that i'm in C++, this does not work anymore.

How's that even possible ? I mean the point of .Net is that Bitmap's inheritences are the same whether i'm in C# or C++ right ?

Maybe i didnt fully grasp the clr thing and how it works. Anyway, thanks in advance for your help.

.h file :

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)
{

}

Error code (in french) :

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

回答1:


A PC reboot did the trick. No idea whatsoever what could have caused this to not work since Bitmap inherits Object...

Anyway, Farewell.



来源:https://stackoverflow.com/questions/44693891/error-c2440-cannot-cast-systemdrawingbitmap-into-systemobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!