C# RichTextbox中插入图片
C# RichTextbox中插入图片程序运行截图:
参考文章: RichTextBox控件查找文本演示,包括查找第一个/下一个/上一个/最后一个. http://www.csframework.com/archive/2/arc-2-20110716-1729.htm 源码: private void btnInsertImg_Click(object sender, EventArgs e) { try { if (File.Exists(txtImgFile.Text)) { Image img = Image.FromFile(txtImgFile.Text); RTB_InsertImg.InsertImage(this.richTextBox1, img); } } catch (Exception ex) { MessageBox.Show("RichTextBox仅支持BMP, JPEG, GIF,ICO,PNG和TIFF格式的图像."); } } RTB_InsertImg.cs using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Drawing; using System.Windows.Forms; using System.Collections.Specialized; using System.Drawing.Imaging; using System.IO; namespace TestProject { /// <summary> /// RichTextBox插入图片使用的静态类 /// 资料来源:www.baidu.com /// </summary> public class RTB_InsertImg { 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 RTB_InsertImg() { 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(); _fontTable.Append(@"{\fonttbl{\f0"); _fontTable.Append(@"\"); if (rtfFontFamily.Contains(_font.FontFamily.Name)) _fontTable.Append(rtfFontFamily[_font.FontFamily.Name]); else _fontTable.Append(rtfFontFamily[FF_UNKNOWN]); _fontTable.Append(@"\fcharset0 "); _fontTable.Append(_font.Name); _fontTable.Append(@";}}"); return _fontTable.ToString(); } /// /// 在RichTextBox当前光标处插入一副图像。 /// /// 多格式文本框控件 /// 插入的图像 public static void InsertImage(RichTextBox rtb, Image image) { StringBuilder _rtf = new StringBuilder(); _rtf.Append(RTF_HEADER); _rtf.Append(GetFontTable(rtb.Font)); _rtf.Append(GetImagePrefix(rtb, image)); _rtf.Append(GetRtfImage(rtb, image)); _rtf.Append(RTF_IMAGE_POST); rtb.SelectedRtf = _rtf.ToString(); } 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(); int picw = (int)Math.Round((_image.Width / xDpi) * HMM_PER_INCH); int pich = (int)Math.Round((_image.Height / yDpi) * HMM_PER_INCH); int picwgoal = (int)Math.Round((_image.Width / xDpi) * TWIPS_PER_INCH); int pichgoal = (int)Math.Round((_image.Height / yDpi) * TWIPS_PER_INCH); _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; MemoryStream _stream = null; Graphics _graphics = null; Metafile _metaFile = null; IntPtr _hdc; try { _rtf = new StringBuilder(); _stream = new MemoryStream(); using (_graphics = rtb.CreateGraphics()) { _hdc = _graphics.GetHdc(); _metaFile = new Metafile(_stream, _hdc); _graphics.ReleaseHdc(_hdc); } using (_graphics = Graphics.FromImage(_metaFile)) { _graphics.DrawImage(_image, new Rectangle(0, 0, _image.Width, _image.Height)); } IntPtr _hEmf = _metaFile.GetHenhmetafile(); uint _bufferSize = GdipEmfToWmfBits(_hEmf, 0, null, MM_ANISOTROPIC, EmfToWmfBitsFlagsDefault); byte[] _buffer = new byte[_bufferSize]; uint _convertedSize = GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer, MM_ANISOTROPIC, EmfToWmfBitsFlagsDefault); 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(); } } } }
参考文档:
C#图片加水印(Generate Watermark Picture) C#插入图片数据并返回自增字段(ISID)的值 C# 实现条码图片自动生成功能 C#图片转换类支持按比例缩放图片 C# Winform 制作可任意拖动的不规则背景图片窗体 C#中将数据插入到Excel表中 C#监控CD-ROM插入/弹出光盘(CDROM_Listener) C#.Net 上传图片,限制图片大小,检查类型完整版 C#切割图片自动填充工具栏按钮图片 C#读取资源文件的图片 C# Winform 拖放图片文件到界面 C# Image图片转Icon图标 C# Dev GridView表格的CustomUnboundColumnData事件异步加载图片类AsynDownImage C# Dev GridView表格使用RepositoryItemPictureEdit显示图标或图片 C# 不占用文件句柄方式加载图片文件(加载到内存流MemoryStream)
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |