WPF RichTextBox可以显示二进制图片内容吗?

发布于 2024-12-18 22:20:11 字数 630 浏览 0 评论 0原文

我试图在新的 WPF 应用程序中显示 RTF 文件(在旧系统中创建),但遇到了一些困难。旧的 RTF 文件包含一个格式为二进制 jpegblip 的图片,当读入 RichTextBox 时,会导致此异常:

Unrecognized structure in data format 'Rich Text Format'. Parameter name: stream

我将文件缩减到最低限度以隔离问题,并最终得到以下开头行(二进制数据已已在本文中删除):

{\rtf1{\pict\picw2700\pich2700\picwgoal2700\pichgoal2700\jpegblip\bin9889

这仍然导致异常,因此我将二进制数据转换为十六进制,并使用开头行创建了一个新文件:

{\rtf1{\pict\picw2700\pich2700\picwgoal2700\pichgoal2700\jpegblip

控件正确显示了其中包含十六进制数据的文件。

有没有人能够将包含二进制图片数据的文件加载到 RichTexBox 控件中,或者如果失败,是否有关于 RTF 规范中 RichTextBox 支持的内容的明确声明?

I am trying to display an RTF file (that was created in a legacy system) in a new WPF application and have run into some difficulty. The old RTF file contains a picture formatted as a binary jpegblip which, when read into the RichTextBox, causes this exception:

Unrecognized structure in data format 'Rich Text Format'. Parameter name: stream

I reduced the file down to the bare minimum to isolate the problem and ended up with the following opening line (the binary data has been removed for this post):

{\rtf1{\pict\picw2700\pich2700\picwgoal2700\pichgoal2700\jpegblip\bin9889

This still caused an exception so I converted the binary data to hex and created a new file with the opening line:

{\rtf1{\pict\picw2700\pich2700\picwgoal2700\pichgoal2700\jpegblip

The file with the hex data in it was displayed correctly by the control.

Has anyone been able to load a file containing binary picture data into the RichTexBox control, or failing that, is there a difinitive statement as to what the RichTextBox supports from the RTF specification?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缪败 2024-12-25 22:20:11

我什至不确定 Microsoft 是否知道 RichTextBox 实现了什么。他们的 RTF 想法不受束缚地漂浮在太空中。请参阅 MS Word、写字板、众多遗留的 Rich Edit 控件等。

其中有一些评论讨论在 .NET 代码中使用 COM 对象,这在某种程度上是可预测的,但 Microsoft 再次没有指定它们在规范的哪些部分跟随。 RichTextBox 控件中的损坏表格(自动换行) - 您可以使用这些在 WPF 中,如果你正确包装它们。

如果您只需显示文档,您可能会询问是否所有用户都将使用 Word。然后您也许可以嵌入 Word 文档查看器,问题很快就解决了。如果您的软件在办公室中使用,那么添加依赖项很容易。

我可能会阅读该格式,直到我知道它是如何工作的。然后在我的程序中,我会将 RTF 文件读入缓冲区,获取原始对象数据,以某种方式对其进行翻译,用更新的对象替换这些对象,然后将其传递给控件。

I'm not even sure Microsoft knows what RichTextBox implements. Their idea of RTF floats around untethered in space. See MS Word, Wordpad, the numerous legacy Rich Edit controls, etc.

This has some comments that talk about using the COM objects in your .NET code, which are somewhat predictable, but again Microsoft doesn't specify what parts of the spec they follow. Broken tables in RichTextBox control (word wrap) - you can use these in WPF if you wrap them properly.

If you just have to display the document, you might ask if all your users will have Word. Then you might be able to embed the Word document viewer, and presto, problem solved. It's an easy dependency to add if your software is used in an office.

I would probably read up on that format until I knew how it worked. Then in my program, I would read the RTF file into a buffer, grab the raw object data, translate it somehow, replace those objects with updated ones, then pass it to the control.

忘你却要生生世世 2024-12-25 22:20:11

对我来说,这段代码工作得很好。我什至可以从数据库表中提取它。

<RichTextBox Height="100" HorizontalAlignment="Left" Margin="306,30,0,0" Name="rtfMain" VerticalAlignment="Top" Width="200" />


rtfMain.Selection.Load(new FileStream(@"C:\temp\document.rtf", FileMode.Open), DataFormats.Rtf);

For me, this code works fine. I can even pull it from a database table too.

<RichTextBox Height="100" HorizontalAlignment="Left" Margin="306,30,0,0" Name="rtfMain" VerticalAlignment="Top" Width="200" />


rtfMain.Selection.Load(new FileStream(@"C:\temp\document.rtf", FileMode.Open), DataFormats.Rtf);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文