[英]PictureBox Drag and Drop to MS Word
我創建了一個帶有圖片框flowlayoutpanel的應用。 我已經根據此處找到的原始代碼實現了拖放:
這又是基於:
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表單的解決方案。
如果您能指出我的想法或建議更改代碼,我將不勝感激。
我為我工作了...您正在另存為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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.