无法将图片插入 RichTextBox

发布于 2024-09-16 06:24:00 字数 941 浏览 3 评论 0原文

我想将图片插入到 RichTextBox 中。我在编码中添加了图片。

这是主要代码,添加 jpg 图像:

MemoryStream memoryStream = new MemoryStream();
img.Save(memoryStream,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = memoryStream.ToArray();
String width = img.Width.ToString();
String height = img.Height.ToString();
String hexImgStr=BitConverter.ToString(bytes, 0).Replace("-","");
String picStr=@"{\pict\jpegblip\picw"+width+@"\pich"+height+
              @"\picwgoal"+width+@"\pichgoal"+height+" "+hexImgStr+"}";

然后我将“picStr”插入到 rtf 文档中。但看不到图像。我认为“hexImgStr”可能是错误的。我还以另一种方式生成“hexImgStr”:

FileStream fs = new FileStream(imgPath,FileMode.Open);
BinaryReader br=new BinaryReader(fs);
//byte[] bytes=new byte[fs.Length];
String hexImgStr="";
for (long i = 0; i < fs.Length; i++)
{
    //bytes[i] = br.ReadByte();
    hexImgStr +=Convert.ToString(br.ReadByte(),16);
}

图像也看不到。有什么问题吗。

多谢。

I want to insert a picture into a RichTextBox. I add the picture in coding.

This is the major code, adding a jpg image:

MemoryStream memoryStream = new MemoryStream();
img.Save(memoryStream,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = memoryStream.ToArray();
String width = img.Width.ToString();
String height = img.Height.ToString();
String hexImgStr=BitConverter.ToString(bytes, 0).Replace("-","");
String picStr=@"{\pict\jpegblip\picw"+width+@"\pich"+height+
              @"\picwgoal"+width+@"\pichgoal"+height+" "+hexImgStr+"}";

Then I insert the "picStr" to the rtf document. But the image can't be seen. I thought "hexImgStr" maybe wrong. I also generate the "hexImgStr" in another way:

FileStream fs = new FileStream(imgPath,FileMode.Open);
BinaryReader br=new BinaryReader(fs);
//byte[] bytes=new byte[fs.Length];
String hexImgStr="";
for (long i = 0; i < fs.Length; i++)
{
    //bytes[i] = br.ReadByte();
    hexImgStr +=Convert.ToString(br.ReadByte(),16);
}

The image can't be seen either. What's wrong with it.

Thanks a lot.

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

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

发布评论

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

评论(2

一腔孤↑勇 2024-09-23 06:24:00

这里失败的可能性很高。首先将 RTF 插入正确的位置。真正的问题可能是您生成的确切格式。图像是 RTB 的嵌入式 OLE 对象,它们需要一个描述该对象的元数据标头。 .NET 中对此没有任何支持,OLE 嵌入很久以前就走上了渡渡鸟的道路。

我知道有效的一件事是通过剪贴板将图像放入 RTB。像这样:

    private void button1_Click(object sender, EventArgs e) {
        using (var img = Image.FromFile("c:\\screenshot.png")) {
            Clipboard.SetImage(img);
            richTextBox1.Paste();
        }
    }

根据需要调整 SelectionStart 属性以确定插入图像的位置。

There's a high probability of failure here. It starts with inserting the RTF in the right place. The true problem is likely to be the exact format you generate. Images are embedded OLE objects for RTB, they need a metadata header that describes the object. There is no support whatsoever for this in .NET, OLE embedding went the way of the dodo a long time ago.

One thing I know that works is to get an image into an RTB through the clipboard. Like this:

    private void button1_Click(object sender, EventArgs e) {
        using (var img = Image.FromFile("c:\\screenshot.png")) {
            Clipboard.SetImage(img);
            richTextBox1.Paste();
        }
    }

Adjust the SelectionStart property as necessary to determine where the image gets inserted.

莫相离 2024-09-23 06:24:00

首先将图片添加到图片框,然后在按钮单击事件中添加以下代码

或手动添加图片......
图像.FromFile("来源");

Clipboard.SetImage(PictureBox1.Image);

this.RichTextBox1.Paste();

First add a picture to picturebox, then add below code inside the button click event

Or add picture manually.....
image.FromFile("Source");

Clipboard.SetImage(PictureBox1.Image);

this.RichTextBox1.Paste();

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