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如何转换类型 每个函数都可以强制将 ...
随机推荐
- Codeforces Round #552 (Div. 3) C. Gourmet Cat (数学,模拟)
题意:你要带着你的喵咪一起去旅行,你的喵在星期\(1,4,7\)吃喵粮\(x\),在星期\(2,6\)吃喵粮\(y\),在星期\(3,5\)吃喵粮\(z\),你只有\(a\)个\(x\),\(b\)个 ...
- 洛谷 P2895 [USACO08FEB]Meteor Shower S (BFS)
题意:你刚开始位于坐标轴的\((0,0)\)点,一共有\(m\)颗流星砸向地面,每颗流星在\(t\)时砸到\((x,y)\)点,其四周上下左右也均有波及,你每秒可以向上下左右移动一个单位,问你是否可以 ...
- typedef struct xxx xxx与struct xxx区别 && “->”和“.”访问结构体变量
1. struct //是C中的结构体的关键词.如: stuct node{ int a;.....} a; node 相当于结构体的类型,关键是其实在C中stuct node 才相当于一个数据类型, ...
- 缓冲区溢出实验 2 sizeof/strlen
实验环境.代码.及准备 https://www.cnblogs.com/lqerio/p/12870834.html vul2 下面只给出有漏洞部分的代码.Vul2问题为sizeof函数在计算时会考虑 ...
- Gym 101480F Frightful Formula(待定系数)题解
#include<cmath> #include<set> #include<map> #include<queue> #include<cstd ...
- WSL2+Terminal+VScode配置调试
最近几天一直想找个方法把VMware虚拟机和远程连接工具MobaXterm这一组配合替换掉,因为每次开启虚拟机操作Ubuntu都需要占用很大的内存,而且要等好久好久才能开启!!!后面还要使用MobaX ...
- Node.js 实战 & 最佳 Express 项目架构
Node.js 实战 & 最佳 Express 项目架构 Express Koa refs https://github.com/xgqfrms/learn-node.js-by-practi ...
- what's the print number means after called the setTimeout function in Chrome console?
what's the print number means after called the setTimeout function in Chrome console? javascript fun ...
- select notes mark
select notes mark mark-line https://time.geekbang.org/column/article/224545
- React In Depth
React In Depth React Component Lifecycle https://reactjs.org/docs/react-component.html https://react ...