如何使用事件或缩放在 richtextbox 中动态调整图像大小

How to dynamically resize image in richtextbox with event or zooming

如何在 richtextbox 中调整 image 的大小。有具体活动吗?

这是我的代码,如何将图像插入 richtextbox。

 private void insetImage_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Bilder (*.png;*.jpg;*.jpeg)|*.png;*.jpg;*.jpeg";
            openFileDialog.Multiselect = true;

            if (openFileDialog.ShowDialog() == true)
            {
                var clipboardData = Clipboard.GetDataObject();
                //BitmapImage bitmapImage = new BitmapImage(new Uri(openFileDialog.FileName, UriKind.Absolute));
                Uri uri = new Uri(openFileDialog.FileName, UriKind.Absolute);
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.UriSource = uri;
            
                bitmapImage.DecodePixelHeight = 200;
                bitmapImage.DecodePixelWidth = 200;

                bitmapImage.EndInit();
                Clipboard.SetImage(bitmapImage);
                richtxtbox.Paste();
                Clipboard.SetDataObject(clipboardData);
        
            }
       }

我在这里将它设为静态,但我希望它在运行时是动态的。

bitmapImage.DecodePixelHeight = 200;

bitmapImage.DecodePixelWidth = 200;

这里是richtextbox

<RichTextBox Grid.Row="2" 
             x:Name="richtxtbox" 
             BorderBrush="#FFF1EDED"
             Block.LineHeight="2"                     
             Padding="2 5"
             SpellCheck.IsEnabled="True" 
             ScrollViewer.VerticalScrollBarVisibility="Visible"
             SelectionChanged="txtEditor_SelectionChanged">
</RichTextBox>

请有人帮忙。谢谢

下面的示例演示了当前选择中所有图像的大小如何增加 25%。

using System.Windows;
using System.Windows.Controls;

namespace WpfApp17
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void rtb_SelectionChanged(object sender, RoutedEventArgs e)
        {
            if (sender is RichTextBox rtb && !rtb.Selection.IsEmpty)
            {
                foreach (Image image in rtb.Selection.FindImages())
                {
                    image.Width *= 1.25;
                    image.Height *= 1.25;
                }
            }
        }
    }
}

用于提取当前选择中的图像列表的扩展方法:

using System.Windows.Documents;
using System.Windows.Controls;
using System.Collections.Generic;

namespace WpfApp17
{
    public static class TextRangeExt
    {
        public static IList<Image> FindImages(this TextRange range)
        {
            IList<Image> images = new List<Image>();
            for (var position = range.Start;
                position != null && position.CompareTo(range.End) <= 0;
                position = position.GetNextContextPosition(LogicalDirection.Forward))
            {
                if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementStart
                    && position.GetAdjacentElement(LogicalDirection.Forward) is InlineUIContainer uic && uic.Child is Image img)
                {
                    images.Add(img);
                }
            }
            return images;
        }
    }
}

xaml:

<Window x:Class="WpfApp17.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"       
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="500">
    <Grid>
        <RichTextBox Name="rtb1" SelectionChanged="rtb_SelectionChanged" Background="LightGray" Foreground="Black">
            <FlowDocument>
            </FlowDocument>
        </RichTextBox>
    </Grid>
</Window>

更新:

以下方法 LoadImage() 可用于从文件加载图像并将其添加到 RichTextBox 中的文档。

public static class RichTextBoxExt
{
    public static void LoadImage(this RichTextBox rtb)
    {
        var image = new Image();

        // Select an Image file
        var dlg = new OpenFileDialog
        {
            Filter = "Image Files(*.bmp;*.jpg;*.gif)|*.bmp;*.jpg;*.gif|All files (*.*)|*.* "
        };
        if (dlg.ShowDialog() == true)
        {
            var imgsrc = new BitmapImage();
            imgsrc.BeginInit();
            imgsrc.StreamSource = File.Open(dlg.FileName, FileMode.Open);
            imgsrc.EndInit();
            image.Source = imgsrc;

            // Set required image size
            image.Height = 200;
            image.Width = 200;

            // Add an selected image to the document
            var para = new Paragraph();
            para.Inlines.Add(image);
            rtb.Document.Blocks.Add(para);
        }
    }
}