有做过RTF的吗,有个插入图片的问题,请大虾帮忙
我希望在我插入的图片的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();
}
}
哪我如何加上图片名呀?
...全文
相关推荐
word模板替换,插入图片
jacob 替换word图片,rtf模板文件字符替换,字符替换成图片,docx4j操作word,itext,poi读取文档图片,条码生成插入文档等... 包和需要的文件都在里面,解压直接可以导入运行
MFC richedit,可进行插入图片、表情,有聊天气泡,与QQ非常类似
MFC richedit,可进行插入图片、表情,有聊天气泡,与QQ非常类似
CRichEdit图片插入复制,RTF格式文件独写,TAB健处理等
开发一个项目需要用到一个CRichEdit控件,基于vs2019的CRichEdit图片插入复制,RTF格式文件独写,TAB健处理等需要的功能都有了。
CRichEdit图片插入复制,RTF格式文件独写、显示行号等
自己开发编译环境使用的,VS2019+WIn32
RichEdit中插入图片,文字以及美化设置
RichEdit中插入图片,文字以及美化设置


