How to convert rtf-formatted text to image?
I have a Windows application (C#) for editing images, and among other features I ca开发者_Python百科n paste from clipboard images or text. When the clipboard contains text, it converts it to a picture (GDI+) with a default font style - than it works like pasting the image. I want to be able to convert some rtf formatted text from the clipboard (as like copied from a formatted source code from VS20xx) to an image to do the same, but preserving fonts, colours (contained tables and images would be also nice!) I can recognise the clipboard content as rtf-formatted text, but I do not have a way to render it as an image! I need to do all that in memory without printing devices and dumping files on disk.
Kinda hacky, but I did something similar to this a long time ago.
01) First create a custom user control inheriting from a rich textbox.
02) Create a bitmap (buffer) with the overall size of the rich textbox. (We're going to use this as a buffer)
03) Override the OnPaint(PaintEventArgs e)
04) Call richtextbox.DrawToBitmap(buffer, Rect(0,0,buffer.Size.Width,buffer.Size.Height)); It will work, but it won't show up in intellisense.
05) On Clipboard command send that to the richtextbox, it should repaint to the buffer.
06) Grab the image buffer and do as you please.
To make sure the OnPaint command gets call, refer to this post Correctly Overriding OnPaint (Rich Textbox)
精彩评论