WPF:允许用户在RichTextBox中调整图像大小 [英] WPF: Allow user to resize images in RichTextBox

查看:283
本文介绍了WPF:允许用户在RichTextBox中调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中的RichTextBox控件中是否有方法允许用户调整插入的图像的大小,或者你必须为此设计自己的方法。

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:

注意:


  • 以纯文本形式读取RTF文件我发现控制标签与图像大小相关的是 \ picscalex100 \ picscaley100 (其中100表示​​缩放为100%)。

  • 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?

推荐答案

原来你需要将你的图像包装在 ResizingAdorner 。

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

可以在 http://msdn.microsoft.com/en-us/library/ms771714%28loband%29.aspx by Marco Zhou(第二篇文章)。

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).

ResizingAdorner 的代码作为MSDN样本在 http://msdn.microsoft.com/en-us/library/ms771714% 28loband%29.aspx

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

这是我正在使用的代码的VB.net等价物

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

ResizingAdorner 示例需要一些很好的黑客来满足我的需求,但这是一个很好的开始。

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

希望别人找到这很有用!

Hope someone else finds this useful!

这篇关于WPF:允许用户在RichTextBox中调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆