此类功能网上很多例子,但转换效果极差,基本看不成。最近用到了这项功能,就写了一个示例。
本示例可将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 ImageList = new List();
文字:
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 ContentList = new List();
转换时,在程序内部建立一个临时的RichTextBox组件(自定宽度,高度会自动计算),装入输入内容,
并计算自适应宽度和高度,然后取得临时RichTextBox内部文字、图像坐标,在一副bitmap上按照坐标绘制文字和图像。
绘制文字使用SQK_Ui.DLL,源码在http://www.haolizi.net/example/view_14426.html,修改源码可调节文字阴影透明度等,
也可以自行搜索DrawStrng用法。
///
/// Rtf 转 图像
///
/// Rtf 内容
/// 图像宽度
/// 每个中文文字的宽度
/// 每个英文文字的宽度
/// 包含的固定样式图像的大小-宽度高度相同
/// 文字的颜色
///
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_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 {
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 }
Rich_tmp.Width = maxlen;
Rich_branch.Dispose();
}