Jar*_*red 4 .net c# rtf richtextbox
我正在创建一个可以轻松插入图像的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("-", "");
}
Run Code Online (Sandbox Code Playgroud)
我已经看到了我正在尝试做的工作示例,但使用wmetafiles,但我不想使用该方法.有任何想法吗?
谢谢,
贾里德
我放弃了尝试手动插入RTF,并决定使用剪贴板方法.我从这种解决方案中找到的唯一不利因素是它消灭了剪贴板内容.我只是在粘贴图像之前保存它们,然后像这样设置它:
internal void InsertImage(Image img)
{
IDataObject obj = Clipboard.GetDataObject();
Clipboard.Clear();
Clipboard.SetImage(img);
this.Paste();
Clipboard.Clear();
Clipboard.SetDataObject(obj);
}
Run Code Online (Sandbox Code Playgroud)
工作得很漂亮.