WPF: Allow user to resize images in RichTextBox

强颜欢笑 提交于 2019-12-03 08:29:32

Turns out you need to wrap your image in a ResizingAdorner.

A beautiful and simple implementation of this code can be found at http://msdn.microsoft.com/en-us/library/ms771714%28loband%29.aspx by Marco Zhou (second post).

The code for this ResizingAdorner is available as an MSDN sample at http://msdn.microsoft.com/en-us/library/ms771714%28loband%29.aspx

Here's a VB.net equivalent of the code I am now using

Dim img As Image
Sub AddImg() Handles btnAddImage.Click
    Dim dlg As New Microsoft.Win32.OpenFileDialog
    dlg.Filter = "Image Files(*.*) | *.*"
    If dlg.ShowDialog Then
        img = New Image
        AddHandler img.Loaded, AddressOf imgloaded
        img.Source = New BitmapImage(New Uri(dlg.FileName, UriKind.Absolute)) With {.CacheOption = BitmapCacheOption.OnLoad}
        Dim container As New BlockUIContainer(img)
        rtb.Document.Blocks.Add(container)
    End If
End Sub

Private Sub imgloaded(ByVal sender As Object, ByVal e As Windows.RoutedEventArgs)
    Dim al As AdornerLayer = AdornerLayer.GetAdornerLayer(img)
    If Not (al Is Nothing) Then
        al.Add(New SDKSample.ResizingAdorner(img))
    End If
End Sub

The ResizingAdorner sample will require some great hacking to meet my needs, but what a great start.

Hope someone else finds this useful!

Maybe copy image to Paint and resize accordingly and then post to the RichTextBox in VB6. Images posted directly to VB6 tend to get distorted. Any image copied from Paint to VB6 is pasted as it was in Paint. I found this out when copying from a PDF image to a RichTextBox.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!