• 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}");
  • 相关阅读:
    useMemo与useCallback
    setState同步异步场景
    useEffect与useLayoutEffect
    Xbatis:SpringBoot 数据管理框架
    Vue3 项目生产环境下如何部署到 Nginx ?
    Config:用户属性配置框架
    50万年薪程序员的面试题
    Spring Security Auth/Acl 实践指南
    工作后,你悟出什么职场道理?
    Json:Java对象和Json文本转换工具类
  • 原文地址:https://www.cnblogs.com/Tangcy/p/7452201.html
Copyright © 2020-2023  润新知