我正在尝试将图像添加到我正在创建的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
}
Run Code Online (Sandbox Code Playgroud)
但问题是这段代码不起作用,因为据我所知,字符串str没有正确的字符串转换在RTF中工作.
编辑:我的问题是在@"\ hex"中的\ hex之后缺少一个空格,并且也没有从BitConverter的返回值中删除" - "字符