WPF实现RichTextBox插入图片及调整行距

WPF里面虽然很多形式上跟Winform一样,但是控件的使用上面还是会有很多诧异。RichTextBox就是一个例子,是的,在WPF里面对这个控件可以做很多Winform很难做的效果出来。

比如在对RichTextBox插入图片,winform时代除了用复制粘贴这种借助剪贴板的差劲方法之外就是要重写和自定义RichTextBox控件了。这就需要高超的编程能力了。但在WPF里面,只需要加几个代码就能搞定了。

在XAML里面添加图片到RichTextBox可以如下所示:

 
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> ​ ​ ​ ​ ​ ​ ​ < R​i​c​h​T​e​x​t​B​o​x​ H​o​r​i​z​o​n​t​a​l​A​l​i​g​n​m​e​n​t =​"​L​e​f​t​" ​M​a​r​g​i​n =​"​9​0​,​1​2​,​0​,​0​" ​N​a​m​e =​"​r​i​c​h​T​e​x​t​B​o​x​1​" > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < R​i​c​h​T​e​x​t​B​o​x​.​D​o​c​u​m​e​n​t > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < F​l​o​w​D​o​c​u​m​e​n​t​ F​o​c​u​s​a​b​l​e =​"​T​r​u​e​" ​L​i​n​e​H​e​i​g​h​t =​"​5​" > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < P​a​r​a​g​r​a​p​h​ x​:​N​a​m​e =​"​g​a​r​a​" > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​文​字​区​域​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < I​m​a​g​e​ S​o​u​r​c​e =​"​D​:​\​1​3​4​2​8​9​2​_​1​0​.​j​p​g​" ​F​o​c​u​s​a​b​l​e =​"​T​r​u​e​" ​H​e​i​g​h​t =​"​5​0​" ​S​t​r​e​t​c​h =​"​U​n​i​f​o​r​m​" /​> ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​文​字​区​域​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < R​u​n​ T​e​x​t =​"​文​字​区​域​文​字​区​域​" >​<​/ R​u​n > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < R​u​n​ T​e​x​t =​"​文​字​区​域​" >​<​/ R​u​n > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ <​/ P​a​r​a​g​r​a​p​h > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < P​a​r​a​g​r​a​p​h​ x​:​N​a​m​e =​"​g​a​r​a​1​" > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < R​u​n​ T​e​x​t =​"​文​字​区​域​" >​<​/ R​u​n > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < R​u​n​ T​e​x​t =​"​文​字​区​域​" >​<​/ R​u​n > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ <​/ P​a​r​a​g​r​a​p​h > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ <​/ F​l​o​w​D​o​c​u​m​e​n​t > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ <​/ R​i​c​h​T​e​x​t​B​o​x​.​D​o​c​u​m​e​n​t > ​ ​ ​ ​ ​ ​ ​ <​/ R​i​c​h​T​e​x​t​B​o​x >

这样就往控件里面添加了图片了。

备注:FlowDocument里面的LineHeight 属性是文字段落的间距。默认间距很大,所以这里调整一下!

当然,这样未必能够完全满足要求,因为有时候我们需要在程序运行的时候点击按钮选取图片进行添加。代码如下:

 
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> p​r​i​v​a​t​e v​o​i​d ​A​d​d​J​P​G​_​C​l​i​c​k​( o​b​j​e​c​t ​s​e​n​d​e​r​,​ ​R​o​u​t​e​d​E​v​e​n​t​A​r​g​s​ ​e​)​ ​ ​ ​ ​ ​ ​ ​ ​{​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ s​t​r​i​n​g ​f​i​l​e​p​a​t​h​ = "​" ;​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ s​t​r​i​n​g ​f​i​l​e​n​a​m​e​ = "​" ;​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​O​p​e​n​F​i​l​e​D​i​a​l​o​g​ ​o​p​e​n​f​i​l​e​j​p​g​ = n​e​w ​O​p​e​n​F​i​l​e​D​i​a​l​o​g​(​)​;​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​o​p​e​n​f​i​l​e​j​p​g​.​F​i​l​t​e​r​ = " j​p​g​图​片​(​*​.​j​p​g​)​|​*​.​j​p​g​|​g​i​f​图​片​(​*​.​g​i​f​)​|​*​.​g​i​f " ;​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​o​p​e​n​f​i​l​e​j​p​g​.​F​i​l​t​e​r​I​n​d​e​x​ = 0 ; openfilejpg.RestoreDirectory = true ; openfilejpg.Multiselect = false ; if (openfilejpg.ShowDialog() == true ) { filepath = openfilejpg.FileName; Image img = new Image(); BitmapImage bImg = new BitmapImage(); img.IsEnabled = true ; bImg.BeginInit(); bImg.UriSource = new Uri(filepath, UriKind.Relative); bImg.EndInit(); img.Source = bImg; // MessageBox.Show(bImg.Width.ToString() + "," + bImg.Height.ToString()); /* 调整图片大小 if (bImg.Height > 100 || bImg.Width > 100) { img.Height = bImg.Height * 0.2; img.Width = bImg.Width * 0.2; } */ img.Stretch = Stretch.Uniform; // 图片缩放模式 new InlineUIContainer(img, richTextBox1.Selection.Start); // 插入图片到选定位置 } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值