Click here to Skip to main content
15,666,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I am creating a program that saves and displays images and a text description. However, I am struggling to discover how to generate the RTF code for the image and text in my program.

I have achieved this manually saving an image/text or both in an editor then opening the file in Notepad. Also, saving the resulting RTF code in Notepad with the .rtf extension creates an RTF file which displays the image and text perfectly in the editor.

I found an example of the function I am looking for (i.e. c# - How can I insert an image into a RichTextBox? - Stack Overflow[^] ), but it's an exec file.

I have never worked with RTF code and have been searching for over a week now. I have read a lot of documentation, but found no clear examples of how this is achieved in code?

Before someone suggested using RTF code I was using the clipboard to paste (as in the code below), but this greatly reduces the image quality. I have found that using RTF code resolves a number of problems:

- Saving multiple items on a form (i.e. Picture Box image and RTF text)
- Opening/extracting multiple items from file
- Reduction in image quality (i.e. resizing, copy and paste, etc.)
- Resizing an image (I also need to achieve this in code, but don't know how to parse the RTF code to alter the picwgoal and pichgoal values?)

The steps I would like to achieve in my program:

- Screenshot and Description: I already have this working. The user captures a screenshot, pastes it into the Picture Box, screenshot is pasted into Rich Text Box(RTB), Dummy Title is appended.
- Generate RTF code: I don't know how to achieve this? I plan to generate RTF code for the image in the Picture Box, paste the code into a hidden RTB, generate RTF code for the Description text, append this code to the image code in the hidden RTB.
- Save the RTF code: Saving the RTF code for the image and text as an .rtf file combines both image and text into one .rtf file! I already have a save routine for saving .rtf files!
- Browse files: Opening the .rtf file displays an image and text in the same file!

If anyone can help me with a example of how to generate/convert RTF code, I would be extremely grateful!

What I have tried:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Check whether PB1 is empty:
    If PictureBox1.Image Is Nothing Then
        'Get image from clipboard to PB1:
        PictureBox1.Image = My.Computer.Clipboard.GetImage
        'Resize image to fit PB1:
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
        'Paste image:
Updated 1-Jun-20 19:15pm

1 solution

The code in GitHub - elistia/DotNetRtfWriter: .NET RTF Writer Library[^] allows you to 'load' an image and then render/'retrieve' the RTF code - I'd have a look at DotNetRtfWriter/RtfImage.cs at master · elistia/DotNetRtfWriter · GitHub[^] ... at a quick glance that's the sort of thing I would do, maybe that library in whole/part might be useful
Share this answer
Member 13032047 8-Jun-20 15:05pm    
Hey Thanks Garth! I did have a quick look at the links you posted, but need to give them a more detailed look! I am actually working on a solution that I plan on posting as soon as I get over some hurdles - i.e. resizing the image, etc. I'm working on that right now but I need some help on using REGEX to achieve this!
Garth J Lancaster 9-Jun-20 2:12am    
REGEX .. Regular Expressions - to resize an image ? Interesting approach, It would not have been my first choice of tools, depending on what you actually mean - good luck

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