4

Is there a method within the RichTextBox control in WPF to allow for the user to resize inserted images, or do you have to devise your own method for this.

What I'm trying to achieve is shown below, a screenshot of a WordPad doing what I want:

enter image description here

Notes:

  • Reading the RTF file as plain text I find that the control tags related to image size is \picscalex100 and \picscaley100 (where 100 denotes scaled to 100%).

So yeah, is there a proper way or trick to this? Any advice on how to go about programming it? Or am I looking at the wrong control altogether?

Jay Wick
  • 12,325
  • 10
  • 54
  • 78

2 Answers2

6

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!

Jay Wick
  • 12,325
  • 10
  • 54
  • 78
  • Where is `SDKSample.ResizingAdorner()` defined? Neither of the links you provided have any examples whatsoever so your answer is not at all useful. – Ortund Jul 13 '17 at 08:53
  • Sorry if it's not helpful but did you try installing the Windows SDK (see [Building the Sample](https://msdn.microsoft.com/en-us/library/ms771714(loband).aspx#Anchor_1)). I honestly have no idea which version, as this was about 7 years ago :( – Jay Wick Jul 13 '17 at 12:51
  • Thats precisely why I commented. If your answer had been more complete, users coming to this answer now would be far less confused about what to do here. – Ortund Jul 13 '17 at 13:14
  • 1
    At [ResizingAdorner](https://social.msdn.microsoft.com/Forums/vstudio/en-US/41229dc6-6171-453b-82bc-f742f629e4f1/richtextbox-resizing-adorner?forum=wpf) I found how to build it. The answer is in C#, but you should be able to convert to VB.NET with [Telerik Converter](http://converter.telerik.com/?utm_medium=product&utm_source=converter&utm_campaign=verA) – D.Kastier Jun 11 '18 at 15:28
-1

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.

Randy
  • 1