PictureBox拖放到MS Word

时间:2018-08-30 13:10:34

标签: vb.net

我创建了一个带有图片框flowlayoutpanel的应用。我已经根据此处找到的原始代码实现了拖放操作:

https://social.msdn.microsoft.com/Forums/en-US/4436369c-eae2-4994-bb64-3c51ada96f07/dragn-drop-from-a-picturebox-to-word?forum=vblanguage

依次基于:

http://groups.google.co.uk/group/microsoft.public.win32.programmer.gdi/browse_thread/thread/197b47c39f8ad2c8/e2e4b88de9f83d3a

 Private Sub P_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles P.MouseDown
        ' see
        ' http://groups.google.co.uk/group/microsoft.public.win32.programmer.gdi/browse_thread/thread/197b47c39f8ad2c8/e2e4b88de9f83d3a
        Dim ms As New MemoryStream
        Dim ms2 As New MemoryStream
        Dim theformat As ImageFormat


        Dim pb = DirectCast(sender, PictureBox)

        Dim ext As String = Path.GetExtension(pb.Tag)

        Select Case ext

            Case ".png"
                theformat = ImageFormat.Png
            Case ".jpg"
                theformat = ImageFormat.Jpeg
            Case ".gif"
                theformat = ImageFormat.Gif
            Case Else
                Return
        End Select



        pb.Image.Save(ms, theformat)
        pb.DoDragDrop(pb.Image, DragDropEffects.Copy)
        Dim bytes() As Byte = ms.GetBuffer
        ms2.Write(bytes, 14, CInt(ms.Length - 14))
        ms.Position = 0
        Dim obj As New DataObject
        obj.SetData("DeviceIndependentBitmap", ms2)
        pb.DoDragDrop(obj, DragDropEffects.Copy)
        ms.Close()
        ms2.Close()
    End Sub

该例程出色地将图像拖放到MS Excel 2013中,但是,我需要将其与MS Word一起使用。当我将图像拖动到Word时,光标会显示正确的“箭头框加号”光标,但是释放鼠标按钮不会删除图像。它也不会引发任何类型的错误。

我很想解决此问题。我一直在寻找修补程序,但主要是找到VB表单到VB表单的解决方案。

如果您能指出我的想法或建议更改代码,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

我为我工作了...您正在另存为PNG / JPG / GIF,都不是DIB(甚至位图)

    Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown

    ' see
    ' http://groups.google.co.uk/group/microsoft.public.win32.programmer.gdi/browse_thread/thread/197b47c39f8ad2c8/e2e4b88de9f83d3a
    Dim ms As New MemoryStream
    Dim ms2 As New MemoryStream


    Dim pb = DirectCast(sender, PictureBox)

    pb.Image.Save(ms, ImageFormat.Bmp)
    pb.DoDragDrop(pb.Image, DragDropEffects.Copy)
    Dim bytes() As Byte = ms.ToArray()
    ms2.Write(bytes, 14, CInt(ms.Length - 14))

    ms.Position = 0
    Dim obj As New DataObject
    obj.SetData("DeviceIndependentBitmap", ms2)
    pb.DoDragDrop(obj, DragDropEffects.Copy)
    ms.Close()

End Sub