我希望在我插入的图片的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();
}
}
哪我如何加上图片名呀?
我的代码是这样的:
//插入图片
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();
}
}
哪我如何加上图片名呀?
解决方案 »
- 求一正则,只保留数字和减号和小数点
- 2010-3-19 14:31:33这个字符串的长度是多少啊?
- 高分求助httpwebrequest问题 急!!
- 错误error CS0123是怎么回事?
- 为什么某一行语句我注释掉了,错误还是出在那一句?******诡异阿******
- 我要把数据表里某一字段数字内容在显示到表格的时候替换成汉字,怎么做?谢谢
- c# 制作音乐播放器的话,如何实现可以把文件直接拖上去播放?
- 请问如何调用.exe文件
- 特别菜的问题
- WPF 如何模板化?
- DATAGRID在ItemDataBound事件触发前怎样将原来的表头去掉?
- 为什么我写入的cookie只有能"http://localhost"里实现,而不能在"http://主机名"里实现
\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 都加不进去。似乎都给屏蔽了