如何将rtf格式的文本转换为图像?
我有一个用于编辑图像的 Windows 应用程序 (C#),除其他功能外,我还可以从剪贴板图像或文本进行粘贴。当剪贴板包含文本时,它会将其转换为具有默认字体样式的图片 (GDI+) - 其工作方式类似于粘贴图像。 我希望能够将剪贴板中的一些 rtf 格式文本(就像从 VS20xx 的格式化源代码复制)转换为图像以执行相同的操作,但保留字体、颜色(包含的表格和图像也很好!) 我可以将剪贴板内容识别为 rtf 格式的文本,但我没有办法将其呈现为图像! 我需要在内存中完成所有这些操作,而不需要打印设备并将文件转储到磁盘上。
I have a Windows application (C#) for editing images, and among other features I can 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有点老套,但我很久以前就做过类似的事情。
01)首先创建一个继承自富文本框的自定义用户控件。
02) 创建一个具有富文本框整体大小的位图(缓冲区)。 (我们将使用它作为缓冲区)
03) 重写 OnPaint(PaintEventArgs e)
04) 调用 richtextbox.DrawToBitmap(buffer, Rect(0,0,buffer.Size.Width,buffer.Size.Height));
它会起作用,但不会显示在智能感知中。
05)在剪贴板命令中将其发送到富文本框,它应该重新绘制到缓冲区。
06) 获取图像缓冲区并按照您的意愿进行操作。
要确保 OnPaint 命令被调用,请参阅这篇文章
正确重写 OnPaint(富文本框)
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)