RichTextBox FlowDocument类型操作_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > RichTextBox FlowDocument类型操作

RichTextBox FlowDocument类型操作

 2017/8/30 13:08:54  唐崇杨  程序员俱乐部  我要评论(0)
  • 摘要:最近研究微信项目,套着web版微信协议做了一个客户端,整体WPF项目MVVM架构及基本代码参考于:http://www.cnblogs.com/ZXdeveloper/archive/2016/11/13/6058206.html由于参考博主的项目没有实现RichTextBox绑定图片与后台接收图片的处理,自己找了一些方法做了一些处理,记录下以防后期用到,或者有人也碰到这个问题提供一些参考。RichTextBox具体值绑定于FlowDocument类型,详细介绍可以查询微软的官方介绍
  • 标签:操作

    最近研究微信项目,套着web版微信协议做了一个客户端,整体WPF项目MVVM架构及基本代码参考于:http://www.cnblogs.com/ZXdeveloper/archive/2016/11/13/6058206.html

由于参考博主的项目没有实现RichTextBox绑定图片与后台接收图片的处理,自己找了一些方法做了一些处理,记录下以防后期用到,或者有人也碰到这个问题提供一些参考。

RichTextBox具体值绑定于FlowDocument类型,详细介绍可以查询微软的官方介绍。

下面正文获取RichTextBox中的图片或者文字以及QQ表情

/// <summary>
        /// 将Document里的值转换成图片或者文字
        /// </summary>
        /// <param name="fld"></param>
        /// <returns></returns>
        public void FlowDocumentMessage(FlowDocument fld)
        {
            if (fld != null)
            {
                string resutStr = string.Empty;
                foreach (var root in fld.Blocks)
                {
                    if (root is BlockUIContainer)
                    {
                        System.Windows.Controls.Image img = (System.Windows.Controls.Image)((BlockUIContainer)root).Child;
                        System.Drawing.Bitmap bitmap = GetBitmap(img);
                    }
                    else
                    {
                        foreach (var item in ((Paragraph)root).Inlines)
                        {
                            //如果是Emoji则进行转换
                            if (item is InlineUIContainer)
                            {
                                System.Windows.Controls.Image img = (System.Windows.Controls.Image)((InlineUIContainer)item).Child;
                                resutStr += GetEmojiName(img.Source.ToString());
                            }
                            //如果是文本,    则直接赋值
                            if (item is Run)
                            {
                                resutStr += ((Run)item).Text;
                            }
                        }
                    }
                }
            }
        }

其中将System.Windows.Controls.Image转成System.Drawing.Imgage也是比较难整,在网上找到一个方法仅供参考

/// <summary>
        /// 将System.Windows.Controls.Image的BitmapSource转换为System.Drawing.Bitmap
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        private System.Drawing.Bitmap GetBitmap(System.Windows.Controls.Image image)
        {
            System.Windows.Media.Imaging.BitmapSource transformedBitmapSource = image.Source as BitmapSource;

            int width = transformedBitmapSource.PixelWidth;
            int height = transformedBitmapSource.PixelHeight;
            int stride = width * ((transformedBitmapSource.Format.BitsPerPixel + 7) / 8);

            byte[] bits = new byte[height * stride];

            transformedBitmapSource.CopyPixels(bits, stride, 0);

            unsafe
            {
                fixed (byte* pBits = bits)
                {
                    IntPtr ptr = new IntPtr(pBits);

                    System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(
                        width,
                        height,
                        stride,
                        System.Drawing.Imaging.PixelFormat.Format32bppPArgb,
                        ptr);
                    return bitmap;
                }
            }
        }

需要更改工程的属性,在工程属性\"生成" 选中 “允许不安全代码”

至于获取QQ表情Emoji名以及绑定到RichTextBox方法可参考文中首行指向的地址博主源码

System.Drawing.Bitmap可以直接保存文件或者转System.Drawing.Imgage,这个就比较简单了 不知道百度即可。

下面是将图片或者文字追加到RichTextBox中

图片:

BlockUIContainer bl = new BlockUIContainer();
                            System.Windows.Controls.Image imgs = new System.Windows.Controls.Image();
                            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(System.Drawing    .Image);
                            BitmapImage bitmapImage = new BitmapImage();
                            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                            {
                                bitmap.Save(ms, ImageFormat.Png);
                                bitmapImage.BeginInit();
                                bitmapImage.StreamSource = ms;
                                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                                bitmapImage.EndInit();
                                bitmapImage.Freeze();
                            }
                            imgs.Source = bitmapImage;
                            imgs.Width = Convert.ToDouble(150);
                            imgs.Height = Convert.ToDouble(100);
                            bl.Child = imgs;
                            RichTextBox.Document.Blocks.Add(bl);
                            RichTextBox.Focus();
                            System.Windows.Forms.SendKeys.SendWait("^{END}");//将光标移动到最后

文字:

Paragraph par = new Paragraph();
            par.Inlines.Add(new Run("文字内容"));
            RichTextBox.Document.Blocks.Add(par);
            RichTextBox.Focus();
            System.Windows.Forms.SendKeys.SendWait("^{END}");

 

发表评论
用户名: 匿名