zoukankan      html  css  js  c++  java
  • WPF往RichTextBox添加图片及调整行距

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

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

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

            <RichTextBox HorizontalAlignment="Left" Margin="90,12,0,0" Name="richTextBox1">

                <RichTextBox.Document>

                    <FlowDocument Focusable="True" LineHeight="5">

                        <Paragraph x:Name="gara">                      

                            文字区域

                            <Image Source="D:\1342892_10.jpg" Focusable="True" Height="50" Stretch="Uniform" />                       

                            文字区域                       

                            <Run Text="文字区域文字区域"></Run>

                            <Run Text="文字区域"></Run>

                        </Paragraph>

                        <Paragraph x:Name="gara1">                      

                            <Run Text="文字区域"></Run>

                            <Run Text="文字区域"></Run>

                        </Paragraph>                   

                    </FlowDocument>

                </RichTextBox.Document>

            </RichTextBox>

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

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

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

    private void AddJPG_Click(object sender, RoutedEventArgs e)

            {

                string filepath = "";

                string filename = "";

                OpenFileDialog openfilejpg = new OpenFileDialog();

                openfilejpg.Filter = "jpg图片(*.jpg)|*.jpg|gif图片(*.gif)|*.gif";

                openfilejpg.FilterIndex = 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); //插入图片到选定位置

                }

            }

    这样就插入了一张图片到RichTextBox里了,是不是很简单呢!

     

    注:文本系原创,如要转载请务必注明作者(梦心)及出处(博客地址:http://www.cnblogs.com/mengxin523/),谢谢!




    -----------------------------------------------------------------------------------------------------------------------------
    SAP ALL进行时...!
    注:本文系原创,如要转载请务必保持原文一致并注明作者(SAP梦心)及出处(博客地址:http://www.cnblogs.com/saper/),违者将会被追究相关责任,谢谢!
  • 相关阅读:
    实现Echarts折线图的虚实转换
    解决Webpack 安装sass时出现的错误
    Videojs视频插件在React中的应用
    overflow应用场景
    【JS Note】字符串截取
    【JS Note】undefined与null
    Web 网页常见问题集锦
    微信内置浏览器中,点击下拉框出现页面乱跳转现象(iphone)
    input美化 checkbox和radio样式
    Discuz!图片查看插件(支持鼠标缩放、实际大小、旋转、下载)
  • 原文地址:https://www.cnblogs.com/saper/p/1706782.html
Copyright © 2011-2022 走看看