【问题标题】:How image included within the text in RichBox in WPF?WPF中RichBox中的文本如何包含图像?
【发布时间】:2016-01-03 15:14:05
【问题描述】:

当在工具里面我想文字包含在图片中我想说话的地方之间,如下图: enter image description here

我尝试了以下代码,但照片没有显示语音,但出现了句尾:

Dim para As New Paragraph()
Dim bitmap As New BitmapImage(New Uri("D:\Happy.png"))
Dim image As New Image()
image.Source = bitmap
image.Width = 20
para.Inlines.Add(image)
RTB.Document.Blocks.Add(para)

【问题讨论】:

    标签: wpf vb.net richtextbox


    【解决方案1】:

    有关示例,请参阅此链接Inline Images or Other Elements

    'A RichTextBox with an image.
    Private Sub ImageRTB()
        'Create a new RichTextBox.
        Dim MyRTB As New RichTextBox()
    
        ' Create a Run of plain text and image.
        Dim myRun As New Run()
        myRun.Text = "Displaying text with inline image"
        Dim MyImage As New Image()
        MyImage.Source = New BitmapImage(New Uri("flower.jpg", UriKind.RelativeOrAbsolute))
        MyImage.Height = 50
        MyImage.Width = 50
        Dim MyUI As New InlineUIContainer()
        MyUI.Child = MyImage
    
        ' Create a paragraph and add the paragraph to the RichTextBox.
        Dim myParagraph As New Paragraph()
        MyRTB.Blocks.Add(myParagraph)
    
        ' Add the Run and image to it.
        myParagraph.Inlines.Add(myRun)
        myParagraph.Inlines.Add(MyUI)
    
        'Add the RichTextBox to the StackPanel.
        MySP.Children.Add(MyRTB)
    End Sub
    

    【讨论】:

    • 谢谢我的朋友,但这不是我想要的。我在窗口和文本中找到的工具,我想在预先存在的文本中插入图像,我不希望通过代码创建新工具并放置文本。
    【解决方案2】:

    找到的解决方案,代码如下:

    Dim tp As TextPointer = rtb.CaretPosition.GetInsertionPosition(LogicalDirection.Forward)
    Dim bm As New BitmapImage()
    bm.BeginInit()
    bm.UriSource = New Uri("Happy.png", UriKind.Relative)
    bm.CacheOption = BitmapCacheOption.OnLoad
    bm.EndInit()
    Dim img As New Image()
    img.Source = bm
    img.Width = 20
    img.Height = 20
    img.Stretch = Stretch.Fill
    Dim container As New InlineUIContainer(img, tp)
    

    谢谢你:)

    【讨论】:

      猜你喜欢
      • 2020-11-23
      • 2020-11-08
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多