如何将图像插入 RichTextBox? [英] How can I insert an image into a RichTextBox?

查看:24
本文介绍了如何将图像插入 RichTextBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到的大多数示例都说要把它放在剪贴板上并使用粘贴,但这似乎不是很好,因为它会覆盖剪贴板.

Most of the examples I see say to put it on the clipboard and use paste, but that doesn't seem to be very good because it overwrites the clipboard.

我确实看到了一种方法,它使用手动将图像放入 RTF将图像转换为 wmf 的 pinvoke.这是最好的方法吗?我还能做更直接的事情吗?

I did see one method that manually put the image into the RTF using a pinvoke to convert the image to a wmf. Is this the best way? Is there any more straightforward thing I can do?

推荐答案

最直接的方法是修改 RTF 代码以自己插入图片.

The most straightforward way would be to modify the RTF code to insert the picture yourself.

在 RTF 中,图片是这样定义的:

In RTF, a picture is defined like this:

'{' pict (brdr? & shading? & picttype & pictsize & metafileinfo?) 数据 '}'问号表示控制字是可选的.数据"只是十六进制格式的文件内容.如果要使用二进制,请使用 in 控制字.

'{' pict (brdr? & shading? & picttype & pictsize & metafileinfo?) data '}' A question mark indicates the control word is optional. "data" is simply the content of the file in hex format. If you want to use binary, use the in control word.

例如:

{pictpngblippicw10449pich3280picwgoal5924pichgoal1860 hex data}
{pictpngblippicw10449pich3280picwgoal5924pichgoal1860in binary data}

pict = 开始一个图片组,pngblip = png 图片picwX = 图片的宽度(X 为像素值)pichX = 图片的高度picwgoalX = 所需的图片宽度(以缇为单位)

pict = starts a picture group, pngblip = png picture picwX = width of the picture (X is the pixel value) pichX = height of the picture picwgoalX = desired width of the picture in twips

因此,要插入图片,您只需要打开图片,将数据转换为十六进制,将这些数据加载到字符串中并在其周围添加 RTF 代码以定义 RTF 图片.现在,您有一个包含图片数据的自包含字符串,您可以将其插入到文档的 RTF 代码中.不要忘记结束}"

So, to insert a picture, you just need to open your picture, convert the data to hex, load these data into a string and add the RTF codes around it to define a RTF picture. Now, you have a self contained string with picture data which you can insert in the RTF code of a document. Don't forget the closing "}"

接下来,从你的RichTextBox(rtbBox.Rtf)中获取RTF代码,在合适的位置插入图片,并设置rtbBox.Rtf的代码

Next, you get the RTF code from your RichTextBox (rtbBox.Rtf), insert the picture at the proper location, and set the code of rtbBox.Rtf

您可能会遇到的一个问题是 .NET RTB 没有很好地支持 RTF 标准.

One issue you may run into is that .NET RTB does not have a very good support of the RTF standard.

我刚刚制作了一个小应用程序*,它允许您快速测试 RTB 中的一些 RTF 代码并查看它如何处理它.您可以在这里下载:RTB 测试员(http://your-translations.com/toys).

I have just made a small application* which allows you to quickly test some RTF code inside a RTB and see how it handles it. You can download it here: RTB tester (http://your-translations.com/toys).

您可以将一些 RTF 内容(例如来自 Word 的)粘贴到左侧的 RTF 框中,然后单击显示 RTF 代码"以在右侧的 RTF 框中显示 RTF 代码,或者您可以将 RTF 代码粘贴到右侧RTB 并单击应用 RTF 代码"以查看左侧的结果.

You can paste some RTF content (from Word, for instance) into the left RTF box and click on the "Show RTF codes" to display the RTF codes in the right RTF box, or you can paste RTF code in the right RTB and click on "Apply RTF codes" to see the results on the left hand side.

您当然可以随意编辑代码,这样可以非常方便地测试 RichTextBox 是否支持您需要的命令,或者学习如何使用 RTF 控制字.

You can of course edit the codes as you like, which makes it quite convenient for testing whether or not the RichTextBox supports the commands you need, or learn how to use the RTF control words.

您可以在线下载 RTF 的完整规范.

You can download a full specification for RTF online.

NB这只是我在5分钟内拍的一个小东西,所以我没有实现文件打开或保存,拖放或其他文明的东西.

NB It's just a little thing I slapped together in 5 minutes, so I didn't implement file open or save, drag and drop, or other civilized stuff.

这篇关于如何将图像插入 RichTextBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆