Click here to Skip to main content
15,563,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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):

'Copy The Image To Clipboard:
        Me.RichTextBox1.[Select](0, 1)

        If (Me.RichTextBox1.SelectionType And RichTextBoxSelectionTypes.Object) = RichTextBoxSelectionTypes.Object Then
        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
Updated 7-Jul-20 10:27am

1 solution


getting things out of a RichTextBox isn't easy.
it has been subject of a question earlier on this very site here:Copy / extract images from a richtextbox[^]

Some facts:
- when you hit the "Print Screen" button an image of type Bitmap is sent to the Clipboard;
- you can find out what is currently on the Clipboard by using some methods/properties of the Clipboard Class
- you can put an image in a PictureBox in two ways, using its Image property (for images in memory) or its ImageLocation property (for files).
- you don't need a PictureBox to display an image; you can set it as a BackgroundImage on most Controls including Panel and Form.
- RichTextBox can return its entire content as a string (Text property even when it includes other items but text) or as a file (SaveFile method); it uses the same .rtf extension Wordpad does (and Word understands). This format is rather cumbersome...
- if examples are using C# don't worry, C# and VB.NET are very similar, it is mainly syntax differences that differentiates them

My conclusion: I would try and avoid RichTextBox if at all possible; when you have an image on the Clipboard you can get it into a PictureBox, a Panel, and more... without going trough the rtf hassles. If you need to extract an image from an existing rtf, use the info I referred to or google some more...

Share this answer
Luc Pattyn 8-Jul-20 11:14am    
OKI suggest you start reading this.

And then you will have to manipulate the rtf string (Text property); you either need to locate, read and fully understand the RTF specification document, or experiment with a few RTF documents you create yourself using WordPad (avoid Word if you can, it always generates a ton of code! Unless you have to support that too...).

I once wrote code that adds an image to an RTF document; it used a Bitmap and some native code to get that converted to MetaFile. I don't have the code for the way back.
Member 13032047 8-Jul-20 11:39am    
Hi Luc, Thanks for your suggestions! The main focus of the app I'm building is the description editor (i.e. RTB) into which a screenshot is pasted, then the user can use a dummy outline to describe the screenshot. The result is saved as an .rtf file. When the .rtf file is opened, I need to extract the image for each individual .rtf file on the fly. Therefore, avoiding the use of an RTB isn't possible. My problem is not assigning an image from the clipboard to a control, but how to get the image from an .rtf file open in the RTB to the clipboard! Once the image is in the clipboard it's not difficult to assign it to a control. Also, I have experimented with my own code and searched the internet for code to discover which type of image is in the open .rtf file, but haven't found any code to achieve this? I will continue looking at the NRTFTree library until I can understand it. But, I still don't have answers to my questions which I feel are essential to achieve what I'm trying to do! If anyone has any code examples I would be very grateful!
Luc Pattyn 8-Jul-20 12:12pm    
As I said before, look at the RTF specification and experiment.
Google brought me this showing there are many possibilities (as always with anything Microsoft) and the story never ends.
Member 13032047 8-Jul-20 12:05pm    
Hi Luc, I looked at the Wikipedia section on images, RTF, etc. I have looked before at the RTF spec and can't seem to glean (or even understand:-) anything from it about images that helps. I already have built a working solution for generating to and converting from RTF Markup code. It works for screenshots (i.e. bitmaps) or web images, but not for jpg's, etc., (an answer I need for the future)! I can both generate the RTF markup from an image together with text in a RTB, clear the RTB and convert from the RTF markup to restore the same image and text to the RTB. I started working on this because someone suggested that it would resolve the issue of image quality when resizing images. This is true and my solution resolves these issues. The app I am now building uses the code from that solution. Extracting just the image from the generated RTF Markup could be a way to display the images from the outlines being browsed in the app, but I have no idea how to achieve this?
Member 13032047 8-Jul-20 12:19pm    
Hey Luc, I'm trying to answer your replies, but all the messages have suddenly disappeared? I looked in config and the preview messages option is checked??? Don't know where to look to see the messages???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900