Resize Images to Fit into Page
Pasted screenshots that exceed the page width can be automatically scaled via the ImageCreated event in TX Text Control for Windows Forms. The handler converts twips to pixels, computes a ratio from available page width minus margins, and applies proportional scaling to each image.

I just got the requirement that pasted screenshots should be resized to fit into the current page. The best and easiest way to realize that is to trap the ImageCreated event. This event returns the image that just has been created.
All we need to do, is to calculate the ratio to resize the image. Therefore, the page width of the current section excluding the page margins must be devided by the width of the image. This factor can be used to scale the image afterwards. The following code shows this ImageCreated event:
private void textControl1_ImageCreated(object sender, TXTextControl.ImageEventArgs e)
{
// get screen resolution to convert twips to pixel
int iTwipsPerPixel = (int)(1440 / textControl1.CreateGraphics().DpiX);
if (((float)(e.Image.Size.Width / iTwipsPerPixel)) >= (textControl1.Sections.GetItem().Format.PageSize.Width - textControl1.Sections.GetItem().Format.PageMargins.Left - textControl1.Sections.GetItem().Format.PageMargins.Right))
{
// resize the image
float fScaleFactor = ((textControl1.Sections.GetItem().Format.PageSize.Width - textControl1.Sections.GetItem().Format.PageMargins.Left - textControl1.Sections.GetItem().Format.PageMargins.Right) / ((float)(e.Image.Size.Width / iTwipsPerPixel))) * 100;
e.Image.VerticalScaling = Convert.ToInt32(fScaleFactor);
e.Image.HorizontalScaling = Convert.ToInt32(fScaleFactor);
}
}
In the following screen-video you can see the sample in action. Feel free to contact me, if you have any questions about this solution.
Related Posts
New Sample: Implementing a 'paste Special' Dialog Box
A Paste Special dialog for TX Text Control lets users select which clipboard format to insert: RTF, plain text, or Unicode text. Since MS Word stores all three simultaneously on copy, the dialog…
Create a Table of Contents in Windows Forms using C#
This article explains how to create a table of contents in Windows Forms using the ribbon or programmatically. Creating a table of contents is required to organize large documents.
Two Ways to Restart Numbered Lists in TX Text Control
In TX Text Control, numbered lists are continued by default and need to be reset when required. There is more than one way if you want to restart numbered lists in a document. In this article, two…
TX Text Control Smart-Tags: Connecting the Controls with Ease
Visual Studio Smart-Tags in TX Text Control .NET for Windows Forms 15.0 provide design-time access to ViewMode, Dock, and Text properties. A built-in wizard automatically arranges ButtonBar,…
Paste Special: The Easy Way to Implement
TX Text Control version 15.0 introduced a ClipboardFormat parameter on the Paste method, enabling native Paste Special functionality. The GetClipboardFormats method returns all available clipboard…
