WPF:允许用户调整 RichTextBox 中图像的大小

发布于 2024-08-17 12:04:54 字数 441 浏览 9 评论 0原文

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

我想要实现的目标如下所示,写字板执行我想要的操作的屏幕截图:

在此处输入图像描述

注意:

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

那么是的,有没有适当的方法或技巧呢?关于如何进行编程有什么建议吗?或者我完全看错了控件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尛丟丟 2024-08-24 12:04:54

结果您需要将图像包装在 ResizingAdorner 中。

可以在 http://msdn.microsoft.com/en-us/library/ms771714%28loband%29.aspx" rel="noreferrer">http://msdn.microsoft.com/zh-cn 找到此代码的美观且简单的实现。 /msdn.microsoft.com/en-us/library/ms771714%28loband%29.aspx 作者:Marco Zhou(第二篇文章)。

ResizingAdorner 的代码可作为 MSDN 示例提供,网址为 http://msdn.microsoft.com/en-us/library/ms771714%28loband%29.aspx

这是我现在使用的代码的 VB.net 等效项

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< /code> 示例需要一些出色的黑客技术才能满足我的需求,但这是一个很棒的开始。

希望其他人发现这很有用!

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!

深者入戏 2024-08-24 12:04:54

也许将图像复制到 Paint 并相应地调整大小,然后发布到 VB6 中的 RichTextBox。直接发布到 VB6 的图像往往会失真。从 Paint 复制到 VB6 的任何图像都会像在 Paint 中一样粘贴。我在从 PDF 图像复制到 RichTextBox 时发现了这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文