2
votes

Here is my current Drag and drop code, I am dragging a image from an email(not an attachment), the cursor changes nicely but when dropped it errors

Unable to cast object of type 'System.IO.MemoryStream' to type 'System.Drawing.Imaging.Metafile[]'.

What am I doing wrong? Is MetafilePict the wrong data format to use?

Private Sub ImageViewer_DragDrop(ByVal sender As System.Object, _
                                 ByVal e As System.Windows.Forms.DragEventArgs) _
                             Handles MyBase.DragDrop

    If (e.Data.GetDataPresent(DataFormats.MetafilePict, False)) Then
        Try
            For Each path As Metafile In CType(e.Data.GetData(DataFormats.MetafilePict), Metafile())
                'Do stuff with data
            Next
        Catch ex As Exception
            MessageBox.Show("Error Doing Drag/Drop")
        End Try
    End If
End Sub

Private Sub ImageViewer_DragEnter(ByVal sender As System.Object, ByVal _
                                  e As System.Windows.Forms.DragEventArgs) _
                              Handles MyBase.DragEnter
    If (e.Data.GetDataPresent(DataFormats.MetafilePict, True)) Then
        e.Effect = DragDropEffects.Copy
    End If
End Sub
1

1 Answers

0
votes

Ok answer to my own question. After a lot of poking around I figured out that this works for dragging images from outlook to vb.net. Since you cannot drag multiple image from outlook I could get rid of the For Each loop.

replace:

If (e.Data.GetDataPresent(DataFormats.MetafilePict, False)) Then
    Try
        For Each path As Metafile In CType(e.Data.GetData(DataFormats.MetafilePict), Metafile())
            'Do stuff with data
        Next

With:

If (e.Data.GetDataPresent(DataFormats.MetafilePict, False)) Then
        Try
            Dim img As Image = DirectCast(e.Data.GetData(DataFormats.Bitmap, True), Image)
            'Do stuff with data