如何调整图像的大小在richtextbox. 是否有特定的事件来完成?
这是我的代码如何插入图像到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。
bitmapImage。
here list 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);
}
}
}