如何在运行时在文本之间将图像插入 WPF RichTextBox 中,以便文本在图像周围浮动 [英] How can I insert an image into a WPF RichTextBox at runtime in between text so the text floats around the image

查看:60
本文介绍了如何在运行时在文本之间将图像插入 WPF RichTextBox 中,以便文本在图像周围浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在运行时将图像插入到 WPF RichTextBox 中的文本之间,以便文本浮动.我尝试使用浮动框,但最终结果是图像旁边只能设置一行,其余内容移动到底部.

I am trying to insert an image into a WPF RichTextBox at runtime in between text so the text floats around. I tried using a floater but the end result is that only one line can be set next to the image and the rest of the content shifts to the bottom.

这是我迄今为止插入图像的代码:

This is the code I have so far for inserting the image:

    private Image SelectImage()
    {
        CommonDialog dialog = new CommonDialog();
        dialog.InitialDirectory =  System.Environment.SpecialFolder.MyDocuments.ToString();
        dialog.Filter.Add( new FilterEntry( Properties.Resources.StrImageFormats, "*.jpg;*.jpeg;*.gif;*.png" ) );
        dialog.Title = Properties.Resources.StrSelectImage;

        if ( dialog.ShowOpen() )
        {
            string filePath = dialog.FileName;
            BitmapImage bitmap = new BitmapImage( new Uri( filePath, UriKind.Absolute ) );
            Image image = new Image();
            image.Source = bitmap;
            image.Width = bitmap.Width;
            image.Height = bitmap.Height;
            return image;
        }
        return null;
    }

    private void ButtonInsertImage_Click( object sender, RoutedEventArgs e )
    {
        Image image = SelectImage();
        if ( image != null )
        {
            TextPointer tp = RTB.CaretPosition.GetInsertionPosition( LogicalDirection.Forward );
            Floater floater = new Floater( new BlockUIContainer( image ), tp );
        }
    }

但是当我将光标置于文本之间时,之前的代码会将图像插入到新行中,而其余文本则位于图像之后.有点像这样:

But when I set my cursor in between the text, the previous code inserts the image on a new line and the rest of the text comes after the image. A little bit like this:

"Lorem ipsum dolor sat amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut Labore et dolore magna aliqua.
中的代表[图片来了]
[图片来了]
[图片来了]
[图片来了]
voluptate velit esse cillum dolore eu fugiat nulla pariatur.例外 sint occaecat cupidatat non proident,sunt in culpa qui officia deserunt mollit anim id estlaborum."

"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
[IMAGE COMES HERE]
[IMAGE COMES HERE]
[IMAGE COMES HERE]
[IMAGE COMES HERE]
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

如何插入图像以使文本围绕图像浮动(图像左右两侧的文本多行)?

How can I insert the image so the text floats around the image (multiple lines off text to the right and the left of the image)?

如果有人知道如何做到这一点,我很乐意在这里介绍.非常感谢.

If anybody has any notion of how to do that, I would love to here it. Thank you soo much.

推荐答案

当您知道如何操作时,这是很容易的方案之一.在您创建 Floater 的代码行之后,添加以下内容:

This is one of those that's easy when you know how. After the line in your code where you create the Floater, add this:

floater.HorizontalAlignment = HorizontalAlignment.Center;
floater.Width = bitmap.Width;

您希望 Floater 根据内容的大小设置其宽度,但由于它可能是您想要换行的文本,因此不能.您必须明确设置宽度.

You'd expect the Floater to set it's width depending on the size of the contents, but since it might be text that you want to wrap, it can't. You have to set the width explicitly.

这篇关于如何在运行时在文本之间将图像插入 WPF RichTextBox 中,以便文本在图像周围浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆