none
RichTextBox插入图片问题 RRS feed

  • 问题

  • BitmapImage bit = new BitmapImage();
    bit.UriSource = new Uri(EMOT_PATH, UriKind.Relative);
    Clipboard.SetImage(bit);
    this.richTextBox.Paste();

    FlowDocument flowDoc = this.richTextBox.Document;
    Block block = flowDoc.Blocks.LastBlock;
    if (block != null && block is Paragraph)
    {
        Paragraph para = block as Paragraph;
        para.Inlines.Add(image);
    }

    两个方法插入图片都不能显示,第二个方法Add到StackPanel就能显示,很奇怪...
    2009年10月16日 2:27

答案

  • 尝试了你的两种方法:

    第一种的确不能显示图片,但是第二种应该是可以显示图片的。当然我们需要确保if条件成立才行。

    请看下我的测试代码:

     Paragraph para1 = new Paragraph();
                BitmapImage bitmap = new BitmapImage(new Uri(@"\tushu\pic\1004.jpg"));
                Image image = new Image();
                image.Source = bitmap;
                para1.Inlines.Add(image);
                fdoc.Blocks.Add(para1);
    Edit:添加XAML 代码:
    <Window x:Class="RichTextBoxInsertImage.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Grid>
            <StackPanel>
            <RichTextBox Name="rtb">
                <RichTextBox.Document>
                    <FlowDocument Name="fdoc"></FlowDocument>
                </RichTextBox.Document>
            </RichTextBox>
                <Button Click="Button_Click">Test</Button>
                </StackPanel>
        </Grid>
    </Window>
    

    测试程序请替换掉图片的路径。

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    2009年10月19日 9:18