无法将类型为'System .__ ComObject'的COM对象转换为类类型为'System.Drawing.Bitmap'的对象. [英] Unable to cast COM oject of type 'System.__ComObject' to class type of 'System.Drawing.Bitmap' .

查看:113
本文介绍了无法将类型为'System .__ ComObject'的COM对象转换为类类型为'System.Drawing.Bitmap'的对象.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void DirectXViewer_DragDrop(object sender, DragEventArgs e)

  {

   mousePressedFlag = false;

   if(e.Data.GetDataPresent(typeof(Bitmap)))

   {

    Object obj = e.Data.GetData(typeof(Bitmap));
    {

     try

     {

      Bitmap image = (Bitmap)((Bitmap)obj).Clone();

      RenderImage = image;

      image.Dispose();

     }
     catch(Exception ex)

        {
              MessageBox.Show(ex.Message.ToString());
        }
    }

   }

  }


private void DirectXViewer_MouseMove(object sender, MouseEventArgs e)

 {

   if (mousePressedFlag)
     {

       if(cacheBitmap != null)
        this.DoDragDrop(new Bitmap(cacheBitmap), DragDropEffects.Copy);
     }
 }



大家好,

我尝试将位图从用户控件拖动到同一控件的另一个实例,但失败.但是,当将位图对象放到拖动对象所在的控件的同一实例时,它就可以工作.当我尝试拖动到其他实例时,会弹出异常:

无法将类型为"System .__ ComObject"的COM对象强制转换为类类型为"System.Drawing.Bitmap"的COM对象.代表COM组件的类型的实例不能转换为不代表COM组件的类型.但是,只要基础COM组件支持对接口IID的QueryInterface调用,就可以将它们强制转换为接口.
在同一过程中拖动两个控件之间的一个位图时起作用.


上面的Bitmap代码块已拖放.非常感谢.请帮助我解决这个问题.



Hi All,

I try to drag an Bitmap from a user control to another instance of the same control but failed. However, it works when the Bitmap object is drop to the same instance of control where it dragged from. When I try to dragged to different instances the exception is popped:

Unable to cast COM oject of type ''System.__ComObject'' to class type of ''System.Drawing.Bitmap'' . Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
It works when drag one Bitmap betweem two control in the same process.


The above code blocks for Bitmap dragged and dropped. Please help me to walkaround the issure, thanks very much.

推荐答案

问题是实际的位图数据不属于拖放到其上的应用程序.对于诸如文本之类的简单内容,这不是问题,因为它不需要特殊处理,因此可以很容易地将其从一个应用程序移至另一个应用程序.虽然我们知道它没有任何可能无法解析的复杂引用,但是像位图对象这样的对象不能轻易地从进程内存复制到进程内存.但是,为了克服这个问题,您必须将位图信息编组到接收方中.该链接提供了执行此操作的代码:

http://stackoverflow.com/questions/1201812/drag-and-drop-between-instances-of-the-same-windows-forms-application [
The problem is that the actual bitmap data is not owned by the application it is dropped onto. For simple stuff like text this isn''t a problem because it doesn''t need special handling so it can be moved from one application to another quite easily. Something like a bitmap object cannot be that easily copied from process memory to process memory, although we would know it doesn''t have any complex references that would be possibly impossible to resolve. But still, to overcome this problem you must marshall the bitmap information into the receiving party. This link provides code to do that:

http://stackoverflow.com/questions/1201812/drag-and-drop-between-instances-of-the-same-windows-forms-application[^]

Good luck!


这篇关于无法将类型为'System .__ ComObject'的COM对象转换为类类型为'System.Drawing.Bitmap'的对象.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆