实例介绍
本示例可将rtf内部文字和标准图像转换为自己可以设定的字体效果和大小的图像。
在自定义的RichTextBox组件输入文字和图像,对每个文字和图像建立坐标和内容
图像:
public class ImageStruct
{
public Point Img_Point;
public int Img_Location;
public Bitmap Img;
public ImageStruct(Point _point, int _location, Bitmap _img)
{
Img_Point = _point;
Img_Location = _location;
Img = _img;
}
}
public List<ImageStruct> ImageList = new List<ImageStruct>();
文字:
public class ContentStruct
{
public Point Content_Point;
public string Content_Text;
public ContentStruct(Point _point, string _text)
{
Content_Point = _point;
Content_Text = _text;
}
}
public List<ContentStruct> ContentList = new List<ContentStruct>();
转换时,在程序内部建立一个临时的RichTextBox组件(自定宽度,高度会自动计算),装入输入内容,
并计算自适应宽度和高度,然后取得临时RichTextBox内部文字、图像坐标,在一副bitmap上按照坐标绘制文字和图像。
绘制文字使用SQK_Ui.DLL,源码在http://www.haolizi.net/example/view_14426.html,修改源码可调节文字阴影透明度等,
也可以自行搜索DrawStrng用法。
/// <summary> /// Rtf 转 图像 /// </summary> /// <param name="Rtf_Message">Rtf 内容</param> /// <param name="_width">图像宽度</param> /// <param name="ch_Width">每个中文文字的宽度</param> /// <param name="en_Width">每个英文文字的宽度</param> /// <param name="img_Width">包含的固定样式图像的大小-宽度高度相同</param> /// <param name="TextColor">文字的颜色</param> /// <returns></returns> private Bitmap RtfToBitmap (string Rtf_Message, int _width, int ch_Width, int en_Width, int img_Width, Color TextColor) { RichTextEx Rich_tmp = new RichTextEx() // 临时Richbox 取得全部内容,重新设置字体 { WordWrap = true, ScrollBars = RichTextBoxScrollBars.None, BorderStyle = BorderStyle.None, Height = 10 }; Rich_tmp.Rtf = Rtf_Message; Rich_tmp.SelectAll(); Rich_tmp.SelectionColor = Color.FromArgb(255, 255, 255); Rich_tmp.SelectionFont = font; int havePic = Regex.Matches(Rich_tmp.Rtf, "Paint.Picture").Count; // 取得包含图像的数量 MatchCollection haveTxt = Regex.Matches(Rich_tmp.Text, @"[\u4e00-\u9fa5]", RegexOptions.IgnoreCase | RegexOptions.Singleline); // 取得包含中文的数量 int haveEn = Rich_tmp.Text.Length - haveTxt.Count; // 取得英文、数字、符号的数量 if ((haveTxt.Count * ch_Width havePic * img_Width haveEn * en_Width) > _width) // 超过限定宽度,为设定的宽度 { Rich_tmp.Width = _width; } else // 没超过限定,为实际宽度 { Rich_tmp.Width = haveTxt.Count * ch_Width havePic * img_Width haveEn * en_Width; } CSetLineSpace.SetLineSpace(Rich_tmp, 390); // 设定行间距 Rich_tmp.updata(); // 刷新 if (Rich_tmp.Lines.Length > 1) //如果有多行,获取行字数最多数的行,设置为图像宽度 { RichTextEx Rich_branch = new RichTextEx() { WordWrap = true, ReadOnly = true, BorderStyle = BorderStyle.None, ScrollBars = RichTextBoxScrollBars.None, Text = Rich_tmp.Text }; for (int t = 0; t < Rich_tmp.ImageList.Count; t ) // 将Rtf内的图像,替换为特殊符号“♐”,避免重复 { Rich_branch.Text = Rich_branch.Text.Remove(Rich_tmp.ImageList[t].Img_Location, 1); Rich_branch.Text = Rich_branch.Text.Insert(Rich_tmp.ImageList[t].Img_Location, "♐"); } MatchCollection haveZh, haveImg; int maxlen = 0, exchange; for (int i = 0; i < Rich_branch.Lines.Length; i ) { haveZh = Regex.Matches(Rich_branch.Lines[i], @"[\u4e00-\u9fa5]", RegexOptions.IgnoreCase | RegexOptions.Singleline); // // Rtf内部中文数量 haveImg = Regex.Matches(Rich_branch.Lines[i], @"♐", RegexOptions.IgnoreCase | RegexOptions.Singleline); // Rtf内部图像的数量 exchange = (Rich_branch.Lines[i].Length - haveZh.Count - haveImg.Count) * en_Width haveZh.Count * ch_Width haveImg.Count * img_Width; // // Rtf内部英文、符号数量 if (exchange > _width) { maxlen = _width; break; } if (maxlen < exchange) maxlen = exchange; } Rich_tmp.Width = maxlen; Rich_branch.Dispose(); }
标签: C# RichTextBox c textbox 图像
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论