如何将图像包含在WPF中的RichBox文本中?

时间:2016-01-02 11:52:52

标签: wpf vb.net richtextbox

当在工具里面时我希望文字包含在我想说的地方之间的图像中,如下图: 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)

2 个答案:

答案 0 :(得分:0)

请参阅此链接以获取示例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

答案 1 :(得分:0)

找到解决方案,这是代码:

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)
谢谢你:)