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}");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM