Search code examples
c#wpf

How to add an image with Resize Adorner to RichTextBox?


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 })));
        }
    }

Solution

  • 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)); };
            }
        }