4

I have an application where users may insert images into a RichTextBox. I'd like to be able to replace all the images in the RTF with some token and store the images in separate files. I'll inject the images back into the RTF later.

I've managed to get the insertion working but ended up resorting to pasting them via the Clipboard (very like Insert an image into RTF document in C#).

The trouble now is how to extract the images.

  1. How do I programatically select an image in a RichTextBox?

  2. Do I have to go back through the clipboard? Something like:

    IDataObject data = Clipboard.GetDataObject();
    Clipboard.Clear();
    
    _RichTextBox.Select(/* The image */);
    _RichTextBox.Copy();
    
    Image img = Clipboard.GetImage();
    img.Save("myImage.png", System.Drawing.Imaging.ImageFormat.Png);
    
    Clipboard.Clear();
    Clipboard.SetDataObject(data);
    
  3. Is there a more elegant solution that doesn't require going through the clipboard?

Thanks for your help!

Community
  • 1
  • 1
Sedes
  • 563
  • 6
  • 11
  • Does [this article](http://www.codeproject.com/KB/edit/csexrichtextbox.aspx) provide any meaningful insights? I don't know the answer to this one, but I am interested in the answer myself. =) – Smudge202 Jun 16 '11 at 19:11

2 Answers2

1

A picture would look like this:

{\*\shppict {\pict \emfblip ..... }}{\nonshppict {\pict ....}}

or even

{\pict ...}

You can check the rtf of the document containing the picture and write a regular expression to extract the images (replacing them with tokens). Another regex replace can restore the images.

Jerry
  • 4,258
  • 3
  • 31
  • 58
0

You can use this RTF Converter to extract the images of a RichTextBox using the class RtfVisualImageAdapter.

Check out the examples:

  • RichTextBox: RtfWinForms (Windows Forms), RtfWindows (WPF)
  • Image Handling: Rtf2Html