以编程方式将图像添加到RTF文档 [英] Programmatically adding Images to RTF Document

查看:188
本文介绍了以编程方式将图像添加到RTF文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像添加到我正在创建的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 +};
返回mpic
}

但问题是此代码不起作用因为据我所知,字符串str没有正确的字符串转换,无法在RTF中工作。



编辑:我的问题是在{ \\ hex在@\ hex中也没有从BitConverter的返回值中删除 - 字符

解决方案

尝试这些链接





<你必须将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图片来源是EMF(增强型图元文件)。
\ pngblip图片来源是PNG。
\ jpegblip图片来源是JPEG。
\shppict指定Word 97-2000图片。这是目标控制字。
\\\
onshppict指定Word 97-2000编写了一个{\ pict目标,它在输入时不会读取。此关键字用于与其他读者兼容。
\macpict图片来源为QuickDraw。
\pmmetafileN图片来源是OS / 2图元文件。 N参数标识元文件类型。 N值在下面的\pmmetafile表中描述。
\wmetafileN图片来源是Windows图元文件。 N参数标识元文件类型(默认值为1)。
\dibitmapN图片来源是Windows设备无关的位图。 N参数标识位图类型(必须等于0)。来自Windows设备无关位图的RTF中包含的信息是BITMAPINFO结构后跟实际像素数据的串联。
\wbitmapN图片来源是Windows设备相关的位图。 N参数标识位图类型(必须等于0)。从Windows设备相关位图包含在RTF中的信息是GetBitmapBits函数的结果。


I am trying to add a image to a RTF document which I am creating. I would prefer to not use 'copy/paste' methods (that involve pasting the image within a RichTextBox and then accessing the .RTF property) which purge the clipboard (as this will be a bother and confusion for my end users).

The code I have so far returns the string that needs to be inserted into the RTF document to print the image. The inputted image (located at $path) is usually in bmp or jpeg format, but at this stage I am not concerned with how the image is stored within the RTF only that i can get it to work.

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
}

However the problem is that this code does not work because as far as I can tell, the string str does not have the correct string conversion to work within the RTF.

Edit: My problem was missing a space after the \hex in @"\hex " and also not stripping out the "-" characters from the returned value of the BitConverter

解决方案

try these links

you must change "picwgoa" to "picwgoal" and "pichgoa" to "pichgoal"

string mpic = @"{\pict\pngblip\picw" + 
    img.Width.ToString() + @"\pich" + img.Height.ToString() +
    @"\picwgoal" + width.ToString() + @"\pichgoal" + height.ToString() + 
    @"\bin " + str + "}";

Here you have a list of the supported image formats

\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.

这篇关于以编程方式将图像添加到RTF文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆