有做过RTF的吗,有个插入图片的问题,请大虾帮忙

csdn5201 2005-09-13 10:14:02
我希望在我插入的图片的RTF中加入图片信息。例如:image1
我的代码是这样的:
//插入图片
public void InsertImage(Image _image ,int _Index) {

StringBuilder _rtf = new StringBuilder();

// Append the RTF header
_rtf.Append(RTF_HEADER);

// Create the font table using the RichTextBox's current font and append
// it to the RTF string
_rtf.Append(GetFontTable(this.Font));

// Create the image control string and append it to the RTF string
_rtf.Append(GetImagePrefix(_image , _Index));

// Create the Windows Metafile and append its bytes in HEX format
_rtf.Append(GetRtfImage(_image));

// Close the RTF image control string
_rtf.Append(RTF_IMAGE_POST);

this.SelectedRtf = _rtf.ToString();
}
//加文件前缀
private string GetImagePrefix(Image _image , int _Index) {

StringBuilder _rtf = new StringBuilder();

// Calculate the current width of the image in (0.01)mm
int picw = (int)Math.Round((_image.Width / xDpi) * HMM_PER_INCH);

// Calculate the current height of the image in (0.01)mm
int pich = (int)Math.Round((_image.Height / yDpi) * HMM_PER_INCH);

// Calculate the target width of the image in twips
int picwgoal = (int)Math.Round((_image.Width / xDpi) * TWIPS_PER_INCH);

// Calculate the target height of the image in twips
int pichgoal = (int)Math.Round((_image.Height / yDpi) * TWIPS_PER_INCH);

// Append values to RTF string
_rtf.Append(@"{\pict\wmetafile8");
_rtf.Append(@"\picw");
_rtf.Append(picw);
_rtf.Append(@"\pich");
_rtf.Append(pich);
_rtf.Append(@"\picwgoal");
_rtf.Append(picwgoal);
_rtf.Append(@"\pichgoal");
_rtf.Append(pichgoal);



// _rtf.Append(@"\imageindex");
// _rtf.Append(_Index.ToString());

_rtf.Append(" ");

return _rtf.ToString();
}

//插图片
private string GetRtfImage(Image _image) {

StringBuilder _rtf = null;

// Used to store the enhanced metafile
MemoryStream _stream = null;

// Used to create the metafile and draw the image
Graphics _graphics = null;

// The enhanced metafile
Metafile _metaFile = null;

// Handle to the device context used to create the metafile
IntPtr _hdc;

try {
_rtf = new StringBuilder();
_stream = new MemoryStream();

// Get a graphics context from the RichTextBox
using(_graphics = this.CreateGraphics()) {

// Get the device context from the graphics context
_hdc = _graphics.GetHdc();

// Create a new Enhanced Metafile from the device context
_metaFile = new Metafile(_stream, _hdc);

// Release the device context
_graphics.ReleaseHdc(_hdc);
}

// Get a graphics context from the Enhanced Metafile
using(_graphics = Graphics.FromImage(_metaFile)) {

// Draw the image on the Enhanced Metafile
_graphics.DrawImage(_image, new Rectangle(0, 0, _image.Width, _image.Height));

}

// Get the handle of the Enhanced Metafile
IntPtr _hEmf = _metaFile.GetHenhmetafile();

// A call to EmfToWmfBits with a null buffer return the size of the
// buffer need to store the WMF bits. Use this to get the buffer
// size.
uint _bufferSize = GdipEmfToWmfBits(_hEmf, 0, null, MM_ANISOTROPIC,
EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);

// Create an array to hold the bits
byte[] _buffer = new byte[_bufferSize];

// A call to EmfToWmfBits with a valid buffer copies the bits into the
// buffer an returns the number of bits in the WMF.
uint _convertedSize = GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer, MM_ANISOTROPIC,
EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);

// Append the bits to the RTF string
for(int i = 0; i < _buffer.Length; ++i) {
_rtf.Append(String.Format("{0:X2}", _buffer[i]));
}

return _rtf.ToString();
}
finally {
if(_graphics != null)
_graphics.Dispose();
if(_metaFile != null)
_metaFile.Dispose();
if(_stream != null)
_stream.Close();
}
}


哪我如何加上图片名呀?
...全文
290 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdn5201 2005-10-24
  • 打赏
  • 举报
回复
没人啊,结分
bobshi 2005-10-24
  • 打赏
  • 举报
回复
不会,帮你up
csdn5201 2005-09-13
  • 打赏
  • 举报
回复
请高手拔刀相助啊!!
csdn5201 2005-09-13
  • 打赏
  • 举报
回复
Picture Data
\binN The picture is in binary format. The numeric parameter N is the number of bytes that follow. Unlike all other controls, this control word takes a 32-bit parameter.
\blipupiN N represents units per inch on a picture (only certain image types need or output this)
\blipuid XXXXX Used as: {\*\blipuid XXXXX} where XXXX is a 16-byte identification number

我使用\blipupi 和 \blipuid 都加不进去。似乎都给屏蔽了
csdn5201 2005-09-13
  • 打赏
  • 举报
回复
怎么没人回呀???都等半天了哇

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧