Resize Images to Fit into Page
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…

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
Copy and paste is probably one of the most common techniques to create letters, emails or other documents. Users are pasting pre-defined boilerplates, text passages from emails or content from…
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
In TX Text Control .NET for Windows Forms 15.0, we introduced the Visual Studio Smart-Tags. This tag is a menu-like interface that supply commonly used design-time options like changing the Dock…
Paste Special: The Easy Way to Implement
In an older sample, I showed how to implement a paste special functionality by accessing the clipboard directly using .NET functionality. In version 15.0, we implemented this functionality…