将图像从outlook拖放到vb.net控件

时间:2013-05-23 16:32:47

标签: vb.net image drag-and-drop outlook

这是我当前的拖放代码,我正在从电子邮件(不是附件)中拖动图像,光标变化很好但是在删除时出现错误

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

我做错了什么? MetafilePict是否使用错误的数据格式?

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 个答案:

答案 0 :(得分:0)

好的回答我自己的问题。经过大量的探索后,我发现这适用于将图像从outlook拖动到vb.net。由于你不能从outlook中拖动多个图像,我可以摆脱For Each循环。

替换:

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

使用:

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