1

this might sound like a repost of a question already asked. However, I have spent a few days looking through and testing many different examples from a number of different websites (incl. stackoveflow.com). Up to now, I got mostly undesired results and am a bit lost as to how to achieve this or something which produces good results? If anyone is able to help I would greatly appreciate it!

I work in VB.NET and use Visual Studio 2015.

My objective is to get a crisp image in the resulting RTF file when I open it in a text editor (I use WPS Office). However, the low quality, out of focus image I have been getting in the final RTF document is not a good header for the description I want to use it for! Nor will it look good after printing! I am not looking for a thumbnail image as the image is required for a larger document header.

Most solutions I have tried deal with resizing an image and then pasting it into a Rich Text Box(RTB).

However, although I have tried using interpolation, antialiasing, etc., the results are not great. For resizing, I have tried examples which use division, percentage, etc.

I am working on an app for my own personal use that "pastes" a screenshot of an active solution/app (i.e. bitmap) into a Picture Box(PB) and into an RTB using the clipboard, title is appended then text added manually in the RTB and the result is saved as an RTF file.

Image Quality issues I have found:

  • Pasting an image after resizing/scaling the original (or a copy) in the Picture Box reduces quality?
  • Saving to an RTF file or opening the RTF file in a text editor reduces quality? (Not sure which causes the issue?)

Both these processes result in a low quality, out of focus image (with or without interpolation, smoothing, antialiasing, etc.)? So, it seems to me that resizing before pasting reduces quality. I have tried pasting directly from the clipboard and also copying the image from the Picture Box to clipboard, then pasting to RTB. Both these procedures produce good results!

To be objective, I cannot share all the code I have tested, but share below a simple example of code which produces the best image quality for me, i.e. just simply pasting the screenshot directly from the clipboard to the RTB. However, the resulting image after pasting into the RTB has not been resized and is therefore too big. I don't want to manually resize the image in the RTF document as this would be an extremely repetitive task!

After pasting the bitmap into the RTF, I have used the Select All or Selection Start - 1 methods to select the image in the RTB, but to frame my question, "How can I resize the image in the RTB?"

You might ask why I want to do this. Well, pasting the bitmap directly into the RTB gives me the same quality image as the one in the Picture Box. There is a slight loss after saving to an RTF file and opening it in a text editor, but the quality is much better than the other methods mentioned above (i.e. resize then paste)! So, I got curious to attempt to paste and then resize! Here is my code:

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:
        RichTextBox1.Paste()

End Sub

  • You can genenrate the RTF markup that defines a Bitmap (or, probably better, an EMF version of it). You can see one method it here: [How can I insert an image into a RichTextBox?](https://stackoverflow.com/a/568378/7444103) and both mthods combined here: [Insert Plain Text and Images into RichTextBox at Runtime](https://www.codeproject.com/Articles/4544/Insert-Plain-Text-and-Images-into-RichTextBox-at-R). – Jimi May 27 '20 at 22:16
  • Hey Jimi, thanks for you quick response! I only just got back to it and saw your answer. I've never used markup or EMF, but I'll take a look at the links you posted and try out the solutions there. I'll get back to you to let you know how I get on! :-) – John Michael Wilkinson May 28 '20 at 13:19
  • This is going to take a while... :-) – John Michael Wilkinson May 28 '20 at 17:13
  • Hi Jimi, getting back to you... I have spent a few hours looking over the webpages at the links you posted, reading RTF documentation (such as RTF Specification), looking for examples of RTF code usage, etc. I have found syntax and command explanations, but nothing to explain how to implement the code to paste an image into an RTB and resize it without losing quality. Most examples in the first link are what I have already tried. Didn't really understand the second link. Could you (or someone else) share a simple example of implementation to get me started? I would be extremely grateful! :-) – John Michael Wilkinson May 28 '20 at 19:45
  • I can reproduce this manually, i.e. generate RTF code saving image/text or both in an editor, then open the file in Notepad. Saving the RTF code in Notepad with the .rtf extension and opening the RTF file in Writer opens an RTF document with image and text. The image quality is what I'm looking for! I have achieved the same result saving the RTF code as an RTF file using my app's RTB and opening the RTF file in Writer. Sylverdrag's exec file in the first link does exactly what I want to achieve. Can anyone share some code to achieve this? Grateful for any help! – John Michael Wilkinson Jun 01 '20 at 16:27

0 Answers0