英文:
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)); };
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论