/// <summary>
/// 获取RTF格式字符串中的16进制图片字符串列表和图片的大小
/// </summary>
/// <param name="rtfText">富文本字符串</param>
/// <returns>图片地址列表</returns>
public static IList<string> GetImageStrList(this string rtfText)
{
string result = rtfText;
//按顺序获取
//16进制图片字符串
IList<string> imageList = new List<string>();
//每个图片的大小
IList<string> imageSize = new List<string>();
//图片地址
IList<string> imagePath = new List<string>();
//获取图片字符串
while (true)
{
int index = 0;
index = rtfText.IndexOf("pichgoal");
if (index == -1) break;
rtfText = rtfText.Remove(0, index + 8);
index = rtfText.IndexOf("\r\n");
if (index > -1) rtfText = rtfText.Remove(0, index);
else break;
index = rtfText.IndexOf("}");
imageList.Add(rtfText.Substring(0, index).Replace("\r\n", ""));
rtfText = rtfText.Remove(0, index);
}
//包含有宽,那就有高
while (result.Contains("\\picw"))
{
//排除干扰选项
string hStr = "pichgoa";
string wStr = "picwgoa";
result = result.Replace(wStr, "");
result = result.Replace(hStr, "");
wStr = "\\picw";
//获取第一个的宽
int index = result.IndexOf(wStr) + wStr.Length;
//只需要有宽高的字符串,其余舍弃
result = result.Substring(index);
int endIndex = result.IndexOf("\\");
wStr = result.Substring(0, endIndex);
//获取第一个的高
index = result.IndexOf("\\pich") + 5;
//只需要有宽高的字符串,其余舍弃
result = result.Substring(index);
endIndex = result.IndexOf("\\");
hStr = result.Substring(0, endIndex);
int width = Convert.ToInt16(wStr);
width = width / 26; //与正常像素大约26倍关系,goal的大约为15被关系
int height = Convert.ToInt16(hStr);
height = height / 26;
imageSize.Add(width + "," + height);
}
Byte[] buffer = null;
for (int i = 0; i != imageList.Count; i++)
{
var size = imageSize[i].Split(',');
int _Count = imageList[i].Length / 2;
buffer = new Byte[imageList[i].Length / 2];
for (int z = 0; z != _Count; z++)
{
//16进制转byte数组
string tempText = imageList[i][z * 2].ToString() + imageList[i][(z * 2) + 1].ToString();
buffer[z] = Convert.ToByte(tempText, 16);
}
MemoryStream ms = new MemoryStream(buffer);
Image img = Image.FromStream(ms);
Bitmap bitMap = new Bitmap(img, int.Parse(size[0]), int.Parse(size[1]));
//保存的路径
string path = "D:"+ "\\" + Guid.NewGuid().ToString() + ".jpg";
bitMap.Save(path);
imagePath.Add(path);
}
return imagePath;
}
RtfRichTextBox 中获取图片大小并保存
最新推荐文章于 2023-06-06 15:09:45 发布