我正在尝试将图像添加到我正在创建的 RTF 文档中。我宁愿不使用清除剪贴板的“复制/粘贴”方法(涉及将图像粘贴到 RichTextBox 中,然后访问 .RTF 属性)(因为这会给我的最终用户带来麻烦和困惑)。
到目前为止,我的代码返回需要插入到 RTF 文档中以打印图像的字符串。输入的图像(位于 $path)通常是 bmp 或 jpeg 格式,但在这个阶段我不关心图像如何存储在 RTF 中,只是我可以让它工作。
public string GetImage(string path, int width, int height)
{
MemoryStream stream = new MemoryStream();
string newPath = Path.Combine(Environment.CurrentDirectory, path);
Image img = Image.FromFile(newPath);
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
byte [] bytes = stream.ToArray();
string str = BitConverter.ToString(bytes, 0).Replace("-", string.Empty);
//string str = System.Text.Encoding.UTF8.GetString(bytes);
string mpic = @"{\pict\pngblip\picw" +
img.Width.ToString() + @"\pich" + img.Height.ToString() +
@"\picwgoa" + width.ToString() + @"\pichgoa" + height.ToString() +
@"\hex " + str + "}";
return mpic
}
然而问题是这个代码不起作用,因为据我所知,字符串 str 没有正确的字符串转换来在 RTF 中工作。
编辑:我的问题是 @"\hex " 中的 \hex 之后缺少一个空格,并且也没有从 BitConverter 的返回值中删除“-”字符
尝试这些链接
- 富文本格式 (RTF) 规范,版本 1.6
- 如何将图像插入 RichTextBox?
- 将图像插入 rtf 文档
您必须将“picwgoa”更改为“picwgoal”,并将“pichgoa”更改为“pichgoal”
string mpic = @"{\pict\pngblip\picw" +
img.Width.ToString() + @"\pich" + img.Height.ToString() +
@"\picwgoal" + width.ToString() + @"\pichgoal" + height.ToString() +
@"\bin " + str + "}";
这里有支持的图像格式列表
\emfblip Source of the picture is an EMF (enhanced metafile).
\pngblip Source of the picture is a PNG.
\jpegblip Source of the picture is a JPEG.
\shppict Specifies a Word 97-2000 picture. This is a destination control word.
\nonshppict Specifies that Word 97-2000 has written a {\pict destination that it will not read on input. This keyword is for compatibility with other readers.
\macpict Source of the picture is QuickDraw.
\pmmetafileN Source of the picture is an OS/2 metafile. The N argument identifies the metafile type. The N values are described in the \pmmetafile table below.
\wmetafileN Source of the picture is a Windows metafile. The N argument identifies the metafile type (the default is 1).
\dibitmapN Source of the picture is a Windows device-independent bitmap. The N argument identifies the bitmap type (must equal 0).The information to be included in RTF from a Windows device-independent bitmap is the concatenation of the BITMAPINFO structure followed by the actual pixel data.
\wbitmapN Source of the picture is a Windows device-dependent bitmap. The N argument identifies the bitmap type (must equal 0).The information to be included in RTF from a Windows device-dependent bitmap is the result of the GetBitmapBits function.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)