WPF: Allow user to resize images in RichTextBox
image, resize, richtextbox, rtf, wpf
Solution
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!
Problem
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: 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?