How can I resize an image in richtextbox. Are there specific events to do it?
here is my code how to insert an image into 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.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
CodePudding user response:
The example below demonstrates how size of all images in the current selection increases by 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
:
<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.
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);
}
}
}