基本信息
源码名称:C# RichTextBox 输入内容转自定义样式图像
源码大小:6.79M
文件格式:.rar
开发语言:C#
更新时间:2018-04-04
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 3 元 
   源码介绍
此类功能网上很多例子,但转换效果极差,基本看不成。最近用到了这项功能,就写了一个示例。

本示例可将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();
            }