在C#中将图像插入RTF文档

发布于 2024-10-21 12:26:07 字数 745 浏览 1 评论 0原文

我正在创建一个可以轻松插入图像的 RichTextBox 子类。我提到这个问题开始,但我无法使生成的 RTF 字符串起作用。当我尝试设置 RTB 的 SelectedRtf 时,出现错误并显示“文件格式无效”。这是我的代码:

internal void InsertImage(Image img)
{
    string str = @"{\pict\pngblip\picw24\pich24 " + imageToHex(img) + "}";

    this.SelectedRtf = str;    // This line throws the exception
}

private string imageToHex(Image img)
{
    MemoryStream ms = new MemoryStream();
    img.Save(ms, ImageFormat.Png);

    byte[] bytes = ms.ToArray();

    string hex = BitConverter.ToString(bytes);
    return hex.Replace("-", "");
}

我已经看到了我想要做的事情的工作示例,但是使用了 wmetafiles,但我不想使用该方法。有什么想法吗?

谢谢,
贾里德

I am creating a RichTextBox subclass that can insert images easily. I referred to this question to start, but I can't get the generated RTF string to work. When I try to set the SelectedRtf of the RTB, it errors out with "File format is not valid." Here is my code:

internal void InsertImage(Image img)
{
    string str = @"{\pict\pngblip\picw24\pich24 " + imageToHex(img) + "}";

    this.SelectedRtf = str;    // This line throws the exception
}

private string imageToHex(Image img)
{
    MemoryStream ms = new MemoryStream();
    img.Save(ms, ImageFormat.Png);

    byte[] bytes = ms.ToArray();

    string hex = BitConverter.ToString(bytes);
    return hex.Replace("-", "");
}

I've seen working examples of what I'm trying to do, but using wmetafiles, but I would prefer not to use that method. Any ideas?

Thanks,
Jared

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

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

发布评论

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

评论(3

眼眸 2024-10-28 12:26:07

我放弃了手动插入 RTF 的尝试,并决定使用剪贴板方法。我从这种类型的解决方案中发现的唯一缺点是它清除了剪贴板内容。我只是在粘贴图像之前保存了它们,然后将其设置回来,如下所示:

internal void InsertImage(Image img)
{
    IDataObject obj = Clipboard.GetDataObject();
    Clipboard.Clear();

    Clipboard.SetImage(img);
    this.Paste();

    Clipboard.Clear();
    Clipboard.SetDataObject(obj);
}

效果很好。

I gave up trying to insert the RTF manually, and decided to use the clipboard approach. The only detriment I could find from this type of solution was that it wiped out the clipboard contents. I simply saved them before I paste the image, then set it back like so:

internal void InsertImage(Image img)
{
    IDataObject obj = Clipboard.GetDataObject();
    Clipboard.Clear();

    Clipboard.SetImage(img);
    this.Paste();

    Clipboard.Clear();
    Clipboard.SetDataObject(obj);
}

Works beautifully.

风流物 2024-10-28 12:26:07

RichTextBox 不支持 PNG,它支持 WMF - 但这不是 C# 中的变体。此外,RichTextBox 支持 BMP 格式的图像 - 这对于 C# 来说是个好主意,因为 Bitmap 是标准的 .Net 类。

RichTextBox doesn't support PNG, it supports WMF - but this isn't variant in C#. Also the RichTextBox supports images in BMP format - this is good idea for C#, because the Bitmap is standard .Net class.

北座城市 2024-10-28 12:26:07

也许这是一种幼稚的方法,但我只是使用写字板将 PNG 插入到 RTF 文档中。下面是第一个块:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\lang9\f0\fs22 testing\par
\par
\pard\sa200\sl240\slmult1{\pict\wmetafile8\picw27940\pich16378\picwgoal8640\pichgoal5070 
0100090000035af60e00000031f60e0000000400000003010800050000000b0200000000050000
000c026b022004030000001e000400000007010400040000000701040031f60e00410b2000cc00
6b022004000000006b0220040000000028000000200400006b020000010018000000000020ec1d
0000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
fffffffffffffffffffffffffffffffffff

正如您所看到的,即使使用 PNG 文件格式,图像头也以 \pict\wmetafile8 开头。尝试将标题更改为该格式,看看是否有效。

Perhaps this is a naive approach, but I just used WordPad to insert a PNG into an RTF document. Below is the first chunk:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\lang9\f0\fs22 testing\par
\par
\pard\sa200\sl240\slmult1{\pict\wmetafile8\picw27940\pich16378\picwgoal8640\pichgoal5070 
0100090000035af60e00000031f60e0000000400000003010800050000000b0200000000050000
000c026b022004030000001e000400000007010400040000000701040031f60e00410b2000cc00
6b022004000000006b0220040000000028000000200400006b020000010018000000000020ec1d
0000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
fffffffffffffffffffffffffffffffffff

As you can see, even with the PNG file format, the image header starts with \pict\wmetafile8. Try changing your header to that format and see if it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文