RtfRichTextBox 中获取图片大小并保存

/// <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;
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Izrj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值