摘要
(Summary
)
在RichTextBox中显示图像是常见的要求,而可用的解决方案有限。 通过剪贴板粘贴或嵌入RTF内容仅支持静态图像。 本文介绍如何将Windows控件对象插入RichTextBox并使用它们托管图像。 这是一个简单但灵活且可用的解决方案。 所附的演示项目显示了一个简单的结果示例:
介绍
(Introduction
)
在EE上,我经常碰到有关如何将图像插入RichTextBox的问题。 特别是对于使用即时消息(IM)项目的开发人员而言,情感图标除了处理聊天文本消息外,也是不可避免的元素。 哭泣的图片听起来比“我在哭”,“我想哭”之类的浅色文本响亮得多。 但是,除非您编写自己的阅读器或浏览器,否则RichTextBox基本上是程序员留下的唯一选择。 使用Web浏览器控件实际上不是一个坏选择。 我们可能会在以下文章(而不是本文)中讨论它。
There are two "standard" ways of displaying static images in a RichTextBox. The CodeProject article, Insert Plain Text and Images into RichTextBox at Runtime, describes two ways of implementation in much detail:
在RichTextBox中显示静态图像有两种“标准”方式。 CodeProject文章“ 在运行时将纯文本和图像插入RichTextBox中”详细介绍了两种实现方式:
Copying an image to the clipboard and pasting the data into target RichTextBox.
将图像复制到剪贴板并将数据粘贴到目标RichTextBox中。
Reading the RTF specification and inserting image data through metafile wrapped with RTF tags.
读取RTF规范并通过带有RTF标签的图元文件插入图像数据。
Actually it’s trivial to restore the original clipboard data after pasting the image and a responsible developer should do it:
实际上,粘贴图像后恢复原始剪贴板数据很简单,负责任的开发人员应该这样做: