Click here to Skip to main content
15,924,367 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
     }
 }


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.
Posted

1 solution

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!
 
Share this answer
 
Comments
QtDot 26-Oct-10 11:20am    
Thanks very much. I have solve the problem and learn a lot from the link you provided. Thanks again, Have a good time~^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900