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}");
RichTextBox FlowDocument类型操作的更多相关文章
- redis 的使用 (sort set排序集合类型操作)
sort set排序集合类型 释义: sort set 是 string 类型的集合 sort set 的每个元素 都会关联一个 权 通过 权值 可以有序的获取集合中的元素 应用场合: 获取热门帖子( ...
- redis 的使用 ( set集合类型操作)
set 集合类型 释义: redis 的 set 是 string 类型的无序集合 set 元素最大可以包含(2的32次方-1)个元素 关于 set 集合类型除了基本的添加删除操 ...
- redis 的使用 ( list列表类型操作)
list 数据类型 列表类型 list 类型是一个双向操作 从链表的头部或者尾部添加删除元素 list 既可以用作栈 也可以用作队列 list 链表的类型应用场合: 获取最新的 10 个用户的信息 s ...
- redis 的使用 (基础, key操作, string类型操作)
使用redis set 类型: 没有重复元素 list 链表类型 有重复累型 sort set 类型 没有重复元素 1.1 存储数据 读取数据 // 数据储存在 内存中 set name laowen ...
- Redis - list类型操作
list类型操作 设置操作:lpush: lpush key value 在list左侧插入value rpush: rpush key value ...
- Redis - set类型操作
set类型操作 设置操作:sadd: sadd key value 给指定的key添加元素,重复返回0表示添加失败. srem: srem key valu ...
- Redis - hash类型操作
hash 类型操作设置操作:hset: hset key filed value 创建指定key的filed-value名值对 hsetnx: hsetnx key file ...
- sql server Geometry 类型操作 笔记
sqlGeometry 类型为sql server 2008之后的版本 新加的一种CLR扩展数据类型,为广大sql server开发人员存储几何类型及空间运算提供极大的便利,下面说明geometry类 ...
- ACCESS中类型操作(限制、转换)
ACCESS如何保留两位小数 1.可以通过修改表结构中字段的“小数位数”即可. 2.可以通过“更新查询”,将所有该字段的值更新为round(字段名,2) ACCESS如何转换类型 每个函数都可以强制将 ...
随机推荐
- Vue的七种传值方式
目录 1,父传子 2,子传父 3,兄弟组件传值 4,父组件使用子组件的数据和方法 5,子组件使用父组件的数据和方法 6,Vuex传值 6.1,定义store 6.2,挂载 6.3,使用 7,路由传值 ...
- DSSM在召回和粗排的应用举例
0.写在前面的话 DSSM(Deep Structured Semantic Models)又称双塔模型,因其结构简单,在推荐系统中应用广泛:下面仅以召回.粗排两个阶段的应用举例,具体描述下DSSM在 ...
- MSE,RMSE
MSE: Mean Squared Error 均方误差是指参数估计值与参数真值之差平方的期望值; MSE可以评价数据的变化程度,MSE的值越小,说明预测模型描述实验数据具有更好的精确度. RMSE ...
- Postman查看上传文件过程时出现400 - Required MultipartFile parameter 'files' is not present错误
我在利用postman查看上传图片文件时,出现了如下图的错误,看到之后很懵逼. 图1 上网搜了一下,归结下来就是参数不一致导致的.不过还有一些是由于没加注解,如下图示: 图2 关于参数不一致问题,主要 ...
- console.dir()可以显示一个对象所有的属性和方法
console.dir()可以显示一个对象所有的属性和方法 具体方法如下:
- xss 之herf输出
首先查看下漏洞页面,发现输入的1111, 直接传参到herf 中, 查阅资料得知: 输出出现在a标签的href属性里面,可以使用javascript协议来执行js 查看源代码: if(isset($ ...
- Qt开发Activex笔记(二):Qt调用Qt开发的Activex控件
若该文为原创文章,转载请注明原文出处本文章博客地址:https://blog.csdn.net/qq21497936/article/details/113789693 长期持续带来更多项目与技术分享 ...
- Pycharm无法import caffe
这里是首先建立在读者可以在终端导入而无法在Pycharm中导入的情况下的: 参考链接(问题的最后一个回答) 选用了虚拟环境的python作为解释器, 但由于caffe的特殊性, 依然没有导入, 原因就 ...
- React 17 All In One
React 17 All In One v17.0.1 https://reactjs.org/blog/2020/10/20/react-v17.html https://reactjs.org/b ...
- HTTPS All In One
HTTPS All In One HTTPS & web security HTTPS Hypertext Transfer Protocol Secure HTTPS is an exten ...