[英]How to dynamically resize image in richtextbox with event or zooming
How can I resize an image in richtextbox .如何调整richtextbox中的图像大小。 Are there specific events to do it?
有具体的活动吗?
here is my code how to insert an image into 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);
}
}
I made it static here but I want it dynamically during runtime.我在这里将其设为静态,但我希望它在运行时动态。
bitmapImage.DecodePixelHeight = 200; bitmapImage.DecodePixelHeight = 200;
bitmapImage.DecodePixelWidth = 200; bitmapImage.DecodePixelWidth = 200;
here ist 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>
please can someone help.请有人帮忙。 Thanks
谢谢
The example below demonstrates how size of all images in the current selection increases by 25%.下面的示例演示了当前选择中所有图像的大小如何增加 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;
}
}
}
}
}
The extension method used to extract images list in the current selection:用于提取当前选择中的图像列表的扩展方法:
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;
}
}
}
The xaml
: 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>
UPDATED:更新:
The following method LoadImage()
might be used to load an image from a file and add it to a document in the RichTextBox.以下方法
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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.