Я пытаюсь добавить изображение в документ RTF, который я создаю. Я бы предпочел не использовать методы "copy/paste" (которые включают вставку изображения в 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