0

Im trying to implement option in richTextBox to show graphical emoticons instead of text representation. So I'm basically looking option to replace eg ':)' string by corresponding picture.

Ive googled a lot and only easy / working solution Ive found was:

Clipboard.SetImage( Properties.Resources.angry  );
richTextBox1.Paste();

But there is big disadvantage of this approach - it overwrites clipboard content, so might interfere user

Can anyone advise how to do that other way (without using clipboard) ?

I was also thinking about saving and restoring clipboard content after operation - but for this I couldn't find easy way either... Any help / tips will be highly appreciated !

EDIT

Actually clipboard backup / restore was bad idea. Noticed sometime is being recognized by OS / AVs as 'dangerous' operation and resulted app crash

Still looking other way / better RTB (free) component ...

EDIT-2

I found working solution here: Insert Image at Cursor Position in Rich Text box

But still not 100% happy because image quality is being lost

Maciej
  • 10,423
  • 17
  • 64
  • 97
  • Via the TextChanged event of the RichTextBox – Lennart Apr 05 '19 at 13:02
  • 1
    You can save the DataObject that's already on the Clipboard, set your thing, then set the DataObject back where you found it. See: [Clipboard.GetDataObject()](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.clipboard.getdataobject) and [Clipboard.SetDataObject()](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.clipboard.setdataobject). Keep in mind that using the .`Paste()` method of the RTB control, you'll lose the bitmap's transparency (if any, of course). – Jimi Apr 05 '19 at 13:03
  • I know those methods, but this doesnt work to me What I see other people have similar issues https://stackoverflow.com/questions/2578900/how-do-i-backup-and-restore-the-system-clipboard-in-c/2579846#2579846 https://stackoverflow.com/questions/1374584/strangeness-with-clipboard-access https://stackoverflow.com/questions/6262454/c-sharp-backing-up-and-restoring-clipboard – Maciej Apr 05 '19 at 22:27
  • 1
    Yes, you can do that. Is it a good idea, in general? No. Do you have other choices? Yes. I think I've already told you in a previous comment that the RTB supports the WMF/EMF format. You **can** embed a Bitmap in a WMF. That's what you should do. If you instead decide to use the Clipboard, do it in a proper way and it will work too (although, there can be more that one corner cases that you need to deal with. Well, as always). You could test the procedure pasting a Bitmap in a RTB and inspecting the RTF. It's all there. – Jimi Apr 06 '19 at 01:01
  • Thanks Jimi. Can you please share some code snippet ? I can convert my PNG to WMF, but still dont know kow to insert WMF / WMF with BMP in.. :( Agree manipulating user clipboard is not best bet... But I cant find other way at this moment... – Maciej Apr 07 '19 at 00:48
  • I missed your comment. What do you need help with? A method to insert, in the existing text, a RTF section that defines the metafile, or to locate the position of the insertion? Both? Something else? – Jimi Apr 07 '19 at 14:01
  • Both actually... (if you can be so kind) – Maciej Apr 08 '19 at 05:54
  • 1
    On your "Edit 2": the "quality loss" you're getting, if it is what the other people there reported, is just transparency information being lost. To get around that, specifically paint the image on a solid background in 32bpp colour mode, and then paint the result of that on the Enhanced Metafile. – Nyerguds Aug 23 '19 at 09:14

1 Answers1

0

After hours of googling and testing seems Ive finally found way how to avoid copy/paste approach.

Below links thanks Ive found my answers:

https://www.codeproject.com/Articles/30902/RichText-Builder-StringBuilder-for-RTF

https://github.com/axuno/SmartFormat/tree/master/src/SmartFormat.Demo/ThirdParty/RTFLib

https://www.bbsmax.com/A/RnJW28Dozq/

Maciej
  • 10,423
  • 17
  • 64
  • 97