When pasting a big image in RichTextBox, it is pasted with its original size. It should be resized so it can fit on the page.
Similar logic is available in the InsertPictureCommand.
Workaround:
private void RadRichTextBox_CommandExecuting(object sender, CommandExecutingEventArgs e)
{
if (!(e.Command is PasteCommand))
{
return;
}
DocumentFragment res = ClipboardEx.GetDocumentFromClipboard("RadDocumentGUID");
if (res == null)
{
foreach (ClipboardHandler settings in ClipboardEx.ClipboardHandlers)
{
res = ClipboardEx.GetDocumentFromClipboard(settings.ClipboardDataFormat, settings.ClipboardStringFilter);
}
}
if (res == null)
{
e.Cancel = true;
var bitmapSource = Clipboard.GetImage();
if (bitmapSource == null)
{
return;
}
Padding sectionmargin = this.radRichTextBox.Document.CaretPosition.GetCurrentSectionBox().AssociatedSection.ActualPageMargin;
double originalPixelWidth = bitmapSource.Width;
double originalPixelHeight = bitmapSource.Height;
if (originalPixelWidth == 0 || originalPixelHeight == 0)
{
originalPixelWidth = 10;
originalPixelHeight = 10;
}
double width = originalPixelWidth;
double height = originalPixelHeight;
if (this.radRichTextBox.Document.LayoutMode == DocumentLayoutMode.Paged)
{
Section currentSection = this.radRichTextBox.Document.CaretPosition.GetCurrentSectionBox().AssociatedSection;
var pageSize = (SizeF) currentSection.GetType().GetProperty("ActualPageSize", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(currentSection, null);
double maxWidth = pageSize.Width - (sectionmargin.Left + sectionmargin.Right);
double maxHeight = pageSize.Height - (sectionmargin.Top + sectionmargin.Bottom);
width = Math.Min(maxWidth, width);
height = Math.Min(maxHeight, height);
}
double ratio = originalPixelWidth / originalPixelHeight;
width = Math.Min(width, height * ratio);
height = width / ratio;
Size size = new Size(width, height);
ImageInline imageInline = new ImageInline(new WriteableBitmap(bitmapSource));
imageInline.Size = size;
this.radRichTextBox.ActiveDocumentEditor.InsertInline(imageInline);
}
}