我怎样才能将图像插入一个RichTextBox? [英] How can I insert an image into a RichTextBox?

查看:286
本文介绍了我怎样才能将图像插入一个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使用的PInvoke将图像转换为wmf的文件。这是最好的办法吗?有没有更简单的事情,我可以做什么?

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 code给自己插入的图片。

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&安培;遮阳和放大器; picttype&放大器; pictsize&放大器; metafileinfo?)数据'}'
问号表示控制字是可选的。
数据仅仅是十六进制格式的文件的内容。如果你想使用的二进制,使用\\ BIN控制字。

'{' \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 \bin control word.

例如:

{\pict\pngblip\picw10449\pich3280\picwgoal5924\pichgoal1860 hex data}
{\pict\pngblip\picw10449\pich3280\picwgoal5924\pichgoal1860\bin 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 codeS围绕它来定义RTF图片。现在,你有画面数据的自包含字符串,就可以在文档的RTF code插入。不要忘了关闭}

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 code,插入在适当的位置的图片,并设置rtbBox.Rtf的code

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 code,看看它是如何处理它。你可以在这里下载:
RTB仪的http://你的 - 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 codeS,在右边RTF框以显示RTF codeS,或者你可以粘贴RTF code在正确的RTB,然后点击应用RTF codeS上看到左手边的结果。

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.

您当然可以编辑codeS,只要你喜欢,这使得用于测试在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天全站免登陆