I need to copy an image from a picture box and text from a textbox to the Windows clipboard together, so that they can be pasted into Word.
I'm using the following code:
Dim bmp As New Bitmap(pb1.Width, pb1.Height) pb1.DrawToBitmap(bmp, New Rectangle(0, 0, pb1.Width, pb1.Height)) Dim dataobj As DataObject = New DataObject dataobj.SetText("This image is a graph") dataobj.SetImage(bmp) Clipboard.SetDataObject(dataobj, True) bmp.Dispose()
Only the text is copied, but commenting out the
SetText line causes the image to be copied. Any help would be much appreciated.
The data formats are for retrieval depending on the context i.e. the bitmap will paste into Paint, and the text will paste into Notepad.
This code demonstrates that you can retrieve them both in .NET. when you specify each format
Dim data As New DataObject() data.SetData(DataFormats.Bitmap, True, Me.pb1.Image) data.SetData(DataFormats.Text, True, "This image is a graph") Clipboard.SetDataObject(data, True) Dim bResult As Bitmap = Clipboard.GetData(DataFormats.Bitmap) Dim tResult As String = Clipboard.GetData(DataFormats.Text)
As another test, put both the image and the text into Word, and copy them both. Then inspect what ends up in
bResult (commenting out the
SetDataObject line of course). You will notice that
tResult has your string, but
bResult is null. You might be able to solve it if you can figure out what format the bitmap is in when it's copied from Word, and use that format when setting it in .NET.
Copying only the image from Word does put a bitmap into
bResult, but when it is copied along with anything else,
Clipboard.GetData(DataFormats.Bitmap) is null.