【发布时间】:2020-01-07 20:16:17
【问题描述】:
我正在尝试在两个程序之间使用拖放图像。在发送应用中
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim drop_effect As DragDropEffects = PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffects.Copy)
End If
End Sub
以及接收应用程序
Private Sub PictureBox1_DragEnter(sender As Object, e As DragEventArgs) Handles PictureBox1.DragEnter
If e.Data.GetDataPresent(DataFormats.Bitmap, True) Then
e.Effect = DragDropEffects.Copy
ElseIf e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub PictureBox1_DragDrop(sender As Object, e As DragEventArgs) Handles PictureBox1.DragDrop
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
PictureBox1.Image =DirectCast(e.Data.GetData(DataFormats.Bitmap), Image)
ElseIf e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim files() As String = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
Dim Bait = My.Computer.FileSystem.ReadAllBytes(files(0))
End If
End Sub
带有长消息的drop stop
System.InvalidCastException:“无法将“System.__ComObject”类型的 COM 对象转换为“System.Drawing.Image”类类型。表示 COM 组件的类型的实例不能转换为不表示 COM 组件的类型;但是,只要底层 COM 组件支持对接口的 IID 的 QueryInterface 调用,它们就可以转换为接口。'
我在互联网上找到了很多示例并尝试过。那些通过编译的人给出了同样的错误。 我尝试的变体是将直接演员表改为尝试演员表或根本不演员表。 请帮助并使用VB。
【问题讨论】:
-
获取异常是很正常的,但它不应该停止。您需要重置调试器,这样它就不会中断您的测试会话。在 VS2017+ 中使用 Debug > Windows > Exception Settings 并取消勾选。
-
我不是在寻找绕过方法,创建自己的格式或使用剪贴板,无论如何我都会添加。
-
我正在寻找答案,为什么这个显示了十几次的过程有效而在这种情况下无效。
-
由于您的图像没有被序列化,当您拖动到当前应用程序范围之外时,Bitmap 对象会进入野外。这会触发通过 COM 打包实现的标准互操作功能。任何允许与外部应用程序交互的 Windows 应用程序都需要在某些时候以某种方式处理 COM 互操作。或者使用众所周知的自定义格式。或者多种众所周知的格式,如果您尝试从 Office 应用程序或 Web 浏览器应用程序中拖动对象,就会发生这种情况:您会看到以 10-20 种不同格式传递的数据。
标签: vb.net image drag-and-drop