資源簡介
此類功能網上很多例子,但轉換效果極差,基本看不成。最近用到了這項功能,就寫了一個示例。
在自定義的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用法。
本示例可將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(); }
代碼片段和文件信息
using?System;
using?System.Drawing;
using?System.Runtime.InteropServices;
using?System.Windows.Forms;
using?System.Drawing.Drawing2D;
using?System.Drawing.Text;
using?System.Text.Regularexpressions;
namespace?test9
{
????public?partial?class?Form1?:?Form
????{
????????//?設置行間距
????????public?class?CSetLineSpace
????????{
????????????public?const?int?WM_USER?=?0x0400;
????????????public?const?int?EM_GETPARAFORMAT?=?WM_USER?+?61;
????????????public?const?int?EM_SETPARAFORMAT?=?WM_USER?+?71;
????????????public?const?long?MAX_TAB_STOPS?=?32;
????????????public?const?uint?PFM_LINESPACING?=?0x00000100;
????????????[StructLayout(LayoutKind.Sequential)]
????????????private?struct?PARAFORMAT2
????????????{
????????????????public?int?cbSize;
????????????????public?uint?dwMas
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????..A..H.?????55296??2018-04-04?11:57??test9\.vs\test9\v15\.suo
?????文件??????????0??2018-04-02?14:40??test9\.vs\test9\v15\Server\sqlite3\db.lock
?????文件?????643072??2018-04-04?11:57??test9\.vs\test9\v15\Server\sqlite3\storage.ide
?????文件????????189??2018-04-02?14:40??test9\test9\App.config
?????文件?????266207??2018-04-04?11:46??test9\test9\bin\Debug\background.jpg
?????文件??????16614??2018-04-03?12:51??test9\test9\bin\Debug\face.png
?????文件?????638976??2018-03-31?19:26??test9\test9\bin\Debug\SQK_Ui.dll
?????文件?????275968??2018-04-04?11:52??test9\test9\bin\Debug\test9.exe
?????文件????????189??2018-04-02?14:40??test9\test9\bin\Debug\test9.exe.config
?????文件??????32256??2018-04-04?11:52??test9\test9\bin\Debug\test9.pdb
?????文件???10828284??2012-04-26?09:53??test9\test9\bin\Debug\中文字體.ttf
?????文件??????11128??2018-04-04?11:52??test9\test9\Form1.cs
?????文件???????3090??2018-04-04?11:52??test9\test9\Form1.Designer.cs
?????文件?????392128??2018-04-04?11:52??test9\test9\Form1.resx
?????文件????????517??2018-04-02?14:40??test9\test9\Program.cs
?????文件???????1308??2018-04-02?14:40??test9\test9\Properties\AssemblyInfo.cs
?????文件???????2823??2018-04-02?14:40??test9\test9\Properties\Resources.Designer.cs
?????文件???????5612??2018-04-02?14:40??test9\test9\Properties\Resources.resx
?????文件???????1092??2018-04-02?14:40??test9\test9\Properties\Settings.Designer.cs
?????文件????????249??2018-04-02?14:40??test9\test9\Properties\Settings.settings
?????文件???????7526??2018-04-04?09:37??test9\test9\RichTextEx.cs
?????文件???????3837??2018-04-04?08:53??test9\test9\test9.csproj
?????文件???????1114??2018-04-02?14:40??test9\test9.sln
?????目錄??????????0??2018-04-04?11:57??test9\.vs\test9\v15\Server\sqlite3
?????目錄??????????0??2018-04-04?08:07??test9\.vs\test9\v15\Server
?????目錄??????????0??2018-04-04?08:07??test9\.vs\test9\v15
?????目錄??????????0??2018-04-04?11:46??test9\test9\bin\Debug
?????目錄??????????0??2018-04-04?08:47??test9\test9\bin\Release
?????目錄??????????0??2018-04-04?11:57??test9\test9\obj\Debug
?????目錄??????????0??2018-04-04?08:07??test9\.vs\test9
............此處省略9個文件信息
- 上一篇:C# 動態繪制視頻(wpf)
- 下一篇:C# 屏幕局域截圖
評論
共有 條評論