Extracting images from RichTextBox

淺唱寂寞╮ 提交于 2019-12-01 09:16:43

问题


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!


回答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.




回答2:


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


来源:https://stackoverflow.com/questions/6376897/extracting-images-from-richtextbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!