What I need to do:
Hi everyone! I need to copy an image to the Clipboard from an .rtf file opened in a Rich Text Box. Then, paste the image to a Picture Box. I have been searching for a long time and copying from a Picture Box(PBX) to paste into a Rich Text Box(RTB) is very common. But, I can't seem to find anything which tells me how to copy and paste the other way except the code I share below? Can anyone help me with this, i.e. an example code, add to the code I shared, a link, etc.? Any help will be greatly appreciated!!! I forgot to mention I'm working in VB.NET, VS2015, and the code I shared is a translation from C#(?).
What I have tried:
First, I paste a screenshot of my app into the RTB. Then, using the following code, I can successfully copy the image to the clipboard and paste it into the PBX. I can also do the same thing copying an image from a webpage, pasting it into the RTB, copy the image to the clipboard, then paste it into the PBX. To test this I capture a screenshot or copy an image from a webpage, paste it into the RTB of my app, clear and check the clipboard is empty to make sure my copy routine is really working, copy the image from the RTB, check the clipboard to ensure the image was copied, then paste the image into the PBX.
However, although this works great for images I just captured in this way it doesn't appear to work for an image contained in an .rtf file I open in my app??? There is a single image and then some text in the .rtf file. The copy routine I'm using actually copies the image in the open .rtf file in the RTB, but doesn't appear to copy the image to the clipboard??? I can now paste the copied image into the RTB creating a duplicate image, but it won't paste into the PBX because pasting depends on the image being in the Clipboard.
My Questions:
1. How do I copy an image (i.e. as per What I need to do (above) to the clipboard and then paste it into a Picture Box?
2. How do I detect what kind of image is in the .rtf file when I open it in my app? I already tried: RichTextBox1.SelectionType.ToString which results in the type: Object. I can also detect the image using: If Description.Rtf.Contains("\pict\wmetafile8\") Then Me.Description.[Select](0, 1), but how do I get the image type to copy it to the Clipboard?
3. When I select and copy the image in the .rtf file, why doesn't it copy to the Clipboard? The Clipboard is empty, yet I can paste the image back into the RTB??? Is the image copied to memory??? How do I get the copied image into the Clipboard? Or can I somehow get the copied image to paste into the PBX???
The Code:
'PROJECT: Copy an image from Richtextbox and paste it in a Picturebox
'SOURCE CODE (Part 1): https:
'Copy The Image To Clipboard:
Clipboard.Clear()
Me.RichTextBox1.[Select](0, 1)
If (Me.RichTextBox1.SelectionType And RichTextBoxSelectionTypes.Object) = RichTextBoxSelectionTypes.Object Then
RichTextBox1.Copy()
End If
'Paste The Image Into PBX From Clipboard:
Dim idata As IDataObject = Clipboard.GetDataObject()
If idata.GetDataPresent("System.Drawing.Bitmap") Then
Dim imgObject As Object = idata.GetData("System.Drawing.Bitmap")
If imgObject IsNot Nothing Then
Dim img As Image = TryCast(imgObject, Image)
If img IsNot Nothing Then
PictureBox1.Image = img
End If
End If
End If