如何在RichTextBox中添加一个带有调整装饰的图像?

huangapple go评论78阅读模式
英文:

How to add an image with Resize Adorner to RichTextBox?

问题

你好,你的代码部分已翻译如下:

我有一个命令,它将图像添加到RichTextBox的FlowDocument中,但当我将它包装在ResizeAdorner中时,图像消失了?如何将带有ResizeAdorner的图像添加到FlowDocument的RichTextBox中

命令

public ICommand ImageCommand { get; set; }
private bool CanImageExecute(object parameter) => true;
private void OnImageExecute(object parameter)
{
    var file = new Microsoft.Win32.OpenFileDialog();
    file.ShowDialog();
    if(System.IO.Path.GetExtension(file.FileName).ToLower() is ".png" || System.IO.Path.GetExtension(file.FileName).ToLower() is ".jpg")
    {
        var BitMapImage = new BitmapImage();
        BitMapImage.BeginInit();
        BitMapImage.StreamSource = new MemoryStream(File.ReadAllBytes(file.FileName));
        BitMapImage.EndInit();

        if (_TextSelected is null) return;
        if (TextSelected.End.Paragraph is null) FileSystem.FileCurent.Blocks.Add(new Paragraph());

        FileSystem.FileCurent.Blocks.InsertBefore(TextSelected.End.Paragraph, new BlockUIContainer(new Image { Source = BitMapImage}));
    }
}

包含包装图像的命令

public ICommand ImageCommand { get; set; }
private bool CanImageExecute(object parameter) => true;
private void OnImageExecute(object parameter)
{
    var file = new Microsoft.Win32.OpenFileDialog();
    file.ShowDialog();
    if(System.IO.Path.GetExtension(file.FileName).ToLower() is ".png" || System.IO.Path.GetExtension(file.FileName).ToLower() is ".jpg")
    {
        var BitMapImage = new BitmapImage();
        BitMapImage.BeginInit();
        BitMapImage.StreamSource = new MemoryStream(File.ReadAllBytes(file.FileName));
        BitMapImage.EndInit();

        if (_TextSelected is null) return;
        if (TextSelected.End.Paragraph is null) FileSystem.FileCurent.Blocks.Add(new Paragraph());

        FileSystem.FileCurent.Blocks.InsertBefore(TextSelected.End.Paragraph, new BlockUIContainer(new ResizeAdorner(new Image { Source = BitMapImage })));
    }
}

如果你需要更多帮助或有其他问题,请随时提出。

英文:

I have a command that adds an image to the FlowDocument of RichTextBox, but when I wrapped it in ResizeAdorner, the image disappeared? How do I add an image with ResizeAdorner to a RichTextBox of FlowDocument?

command

 public ICommand ImageCommand { get; set; }
    private bool CanImageExecute(object parameter) => true;
    private void OnImageExecute(object parameter)
    {
        var file = new Microsoft.Win32.OpenFileDialog();
        file.ShowDialog();
        if(System.IO.Path.GetExtension(file.FileName).ToLower() is ".png" || System.IO.Path.GetExtension(file.FileName).ToLower() is ".jpg")
        {
            var BitMapImage = new BitmapImage();
            BitMapImage.BeginInit();
            BitMapImage.StreamSource = new MemoryStream(File.ReadAllBytes(file.FileName));
            BitMapImage.EndInit();

            if (_TextSelected is null) return;
            if (TextSelected.End.Paragraph is null) FileSystem.FileCurent.Blocks.Add(new Paragraph());

            FileSystem.FileCurent.Blocks.InsertBefore(TextSelected.End.Paragraph, new BlockUIContainer(new Image { Source = BitMapImage}));
        }
    }

command with wrapped image

 public ICommand ImageCommand { get; set; }
    private bool CanImageExecute(object parameter) => true;
    private void OnImageExecute(object parameter)
    {
        var file = new Microsoft.Win32.OpenFileDialog();
        file.ShowDialog();
        if(System.IO.Path.GetExtension(file.FileName).ToLower() is ".png" || System.IO.Path.GetExtension(file.FileName).ToLower() is ".jpg")
        {
            var BitMapImage = new BitmapImage();
            BitMapImage.BeginInit();
            BitMapImage.StreamSource = new MemoryStream(File.ReadAllBytes(file.FileName));
            BitMapImage.EndInit();

            if (_TextSelected is null) return;
            if (TextSelected.End.Paragraph is null) FileSystem.FileCurent.Blocks.Add(new Paragraph());

            FileSystem.FileCurent.Blocks.InsertBefore(TextSelected.End.Paragraph, new BlockUIContainer(new ResizeAdorner(new Image { Source = BitMapImage })));
        }
    }

答案1

得分: 2

我解决了这个问题。问题在于只有在加载组件后才能获取到 AdornerLayer

这是有效的命令:

public ICommand ImageCommand { get; set; }
private bool CanImageExecute(object parameter) => true;
private void OnImageExecute(object parameter)
{
    var file = new Microsoft.Win32.OpenFileDialog();
    file.ShowDialog();
    if (System.IO.Path.GetExtension(file.FileName).ToLower() is ".png" || System.IO.Path.GetExtension(file.FileName).ToLower() is ".jpg")
    {
        var BitMapImage = new BitmapImage();
        BitMapImage.BeginInit();
        BitMapImage.StreamSource = new MemoryStream(File.ReadAllBytes(file.FileName));
        BitMapImage.EndInit();

        if (_TextSelected is null) return;
        if (TextSelected.End.Paragraph is null) FileSystem.FileCurent.Blocks.Add(new Paragraph());
        var cont = new Border { Width = 100, Height = 100, Child = new Image { Source = BitMapImage } };
        FileSystem.FileCurent.Blocks.InsertBefore(TextSelected.End.Paragraph, new Paragraph(new InlineUIContainer(cont)));
        cont.Loaded += (s, e) => { AdornerLayer.GetAdornerLayer((Grid)parameter).Add(new ResizeAdorner(cont)); };
    }
}
英文:

I solved the problem. The thing was that AdornerLayer can be obtained only after loading the component.

Here is the working command

 public ICommand ImageCommand { get; set; }
    private bool CanImageExecute(object parameter) => true;
    private void OnImageExecute(object parameter)
    {
        var file = new Microsoft.Win32.OpenFileDialog();
        file.ShowDialog();
        if(System.IO.Path.GetExtension(file.FileName).ToLower() is ".png" || System.IO.Path.GetExtension(file.FileName).ToLower() is ".jpg")
        {
            var BitMapImage = new BitmapImage();
            BitMapImage.BeginInit();
            BitMapImage.StreamSource = new MemoryStream(File.ReadAllBytes(file.FileName));
            BitMapImage.EndInit();

            if (_TextSelected is null) return;
            if (TextSelected.End.Paragraph is null) FileSystem.FileCurent.Blocks.Add(new Paragraph());
            var cont = new Border { Width = 100, Height = 100, Child = new Image { Source = BitMapImage } };
            FileSystem.FileCurent.Blocks.InsertBefore(TextSelected.End.Paragraph, new Paragraph( new InlineUIContainer(cont)));
            cont.Loaded += (s, e) => { AdornerLayer.GetAdornerLayer((Grid)parameter).Add(new ResizeAdorner(cont)); };
        }
    }

huangapple
  • 本文由 发表于 2023年5月7日 21:39:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定