richTextBox里面可以放各种类型的文字信息,如何插入图片呢?

解决方案 »

  1.   

    Image img = Image.FromFile("pic.jpg");
    Clipboard.SetDataObject(img);
    this.richTextBox.Paste(DataFormats.GetFormat(DataFormats.Bitmap));
      

  2.   

    public class RichTextBox_InsPic
    {
    public const int
    EmfToWmfBitsFlagsDefault = 0x00000000,
    EmfToWmfBitsFlagsEmbedEmf = 0x00000001,
    EmfToWmfBitsFlagsIncludePlaceable = 0x00000002,
    EmfToWmfBitsFlagsNoXORClip = 0x00000004; [DllImport("gdiplus.dll")]
    public static extern uint GdipEmfToWmfBits(IntPtr _hEmf, uint _bufferSize, byte[] _buffer, int _mappingMode, int _flags); private struct RtfFontFamilyDef 
    {
    public const string Unknown = @"\fnil";
    public const string Roman = @"\froman";
    public const string Swiss = @"\fswiss";
    public const string Modern = @"\fmodern";
    public const string Script = @"\fscript";
    public const string Decor = @"\fdecor";
    public const string Technical = @"\ftech";
    public const string BiDirect = @"\fbidi";
    } private const int MM_ISOTROPIC = 7;
    private const int MM_ANISOTROPIC = 8;
    private const int HMM_PER_INCH = 2540;
    private const int TWIPS_PER_INCH = 1440; private const string FF_UNKNOWN = "UNKNOWN"; private const string RTF_HEADER = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033";
    private const string RTF_DOCUMENT_PRE = @"\viewkind4\uc1\pard\cf1\f0\fs20";
    private const string RTF_DOCUMENT_POST = @"\cf0\fs17}";
    private const string RTF_IMAGE_POST = @"}"; private static HybridDictionary rtfFontFamily; static RichTextBox_InsPic()
    {
    rtfFontFamily = new HybridDictionary();
    rtfFontFamily.Add(FontFamily.GenericMonospace.Name, RtfFontFamilyDef.Modern);
    rtfFontFamily.Add(FontFamily.GenericSansSerif.Name, RtfFontFamilyDef.Swiss);
    rtfFontFamily.Add(FontFamily.GenericSerif.Name, RtfFontFamilyDef.Roman);
    rtfFontFamily.Add(FF_UNKNOWN, RtfFontFamilyDef.Unknown);
    } private static string GetFontTable(Font _font) 
    {
    StringBuilder _fontTable = new StringBuilder();
    // Append table control string
    _fontTable.Append(@"{\fonttbl{\f0");
    _fontTable.Append(@"\");
    // If the font's family corresponds to an RTF family, append the
    // RTF family name, else, append the RTF for unknown font family.
    if (rtfFontFamily.Contains(_font.FontFamily.Name))
    _fontTable.Append(rtfFontFamily[_font.FontFamily.Name]);
    else
    _fontTable.Append(rtfFontFamily[FF_UNKNOWN]);
    // \fcharset specifies the character set of a font in the font table.
    // 0 is for ANSI.
    _fontTable.Append(@"\fcharset0 ");
    // Append the name of the font
    _fontTable.Append(_font.Name);
    // Close control string
    _fontTable.Append(@";}}");
    return _fontTable.ToString();
    } /// <summary>
    /// 在RichTextBox当前光标处插入一副图像。
    /// </summary>
    /// <param name="rtb">多格式文本框控件</param>
    /// <param name="image">插入的图像</param>
    public static void InsertImage(RichTextBox rtb, Image image)
    {
    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(rtb.Font));
    // Create the image control string and append it to the RTF string
    _rtf.Append(GetImagePrefix(rtb, image));
    // Create the Windows Metafile and append its bytes in HEX format
    _rtf.Append(GetRtfImage(rtb, image));
    // Close the RTF image control string
    _rtf.Append(RTF_IMAGE_POST);
    rtb.SelectedRtf = _rtf.ToString();
    }
    /// <summary>
    /// 在RichTextBox当前光标处插入一段文本,使这段文本的样式与超连接相同。
    /// </summary>
    /// <param name="rtb">多格式文本框控件</param>
    /// <param name="text">插入的文本</param>
    private static string GetImagePrefix(RichTextBox rtb, Image _image) 
    {
    float xDpi;
    float yDpi;
    using(Graphics _graphics = rtb.CreateGraphics()) 
    {
    xDpi = _graphics.DpiX;
    yDpi = _graphics.DpiY;
    } 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(" ");
    return _rtf.ToString();
    } private static string GetRtfImage(RichTextBox rtb, 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 = rtb.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, 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, 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();
    }
    }
    }
      

  3.   

    给你两个,仅供参考,选择:
    http://www.codeproject.com/cs/miscctrl/MyExtRichTextBox.asp
    http://www.codeproject.com/cs/miscctrl/csexrichtextbox.asp