# 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.

- **Author:** Bjoern Meyer
- **Published:** 2008-05-08
- **Modified:** 2026-03-05
- **Description:** 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.
- **2 min read** (295 words)
- **Tags:**
  - .NET
  - Sample
  - Video
- **Web URL:** https://www.textcontrol.com/blog/2008/05/08/resize-images-to-fit-into-page/
- **LLMs URL:** https://www.textcontrol.com/blog/2008/05/08/resize-images-to-fit-into-page/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2008/05/08/resize-images-to-fit-into-page/llms-full.txt

---

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](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.textcontrol.imagecreated.event.htm) 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.

var flashvars = {}, params = {wmode: "transparent"}, attributes = {};
swfobject.embedSWF("https://s1-www.textcontrol.com/assets/dist/blog/2008/05/08/a/assets/movie.swf", "o3633895d258261437c93d8a3195d0b4adb0372be03f7f7d0411f7bd431fcab91", "400", "318", "9.0.0", "https://s1-www.textcontrol.com/assets/dist/blog/2008/05/08/a/assets/swfobject/expressInstall.swf", flashvars, params, attributes);
[![Get Adobe Flash player](http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif)](http://www.adobe.com/go/getflashplayer)

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [New Sample: Implementing a 'paste Special' Dialog Box](https://www.textcontrol.com/blog/2008/07/10/new-sample-implementing-a-paste-special-dialog-box/llms.txt)
- [Create a Table of Contents in Windows Forms using C#](https://www.textcontrol.com/blog/2023/01/23/create-toc-in-windows-forms/llms.txt)
- [Two Ways to Restart Numbered Lists in TX Text Control](https://www.textcontrol.com/blog/2021/11/03/two-ways-to-restart-numbered-lists/llms.txt)
- [TX Text Control Smart-Tags: Connecting the Controls with Ease](https://www.textcontrol.com/blog/2009/10/16/tx-text-control-smart-tags-connecting-the-controls-with-ease/llms.txt)
- [Paste Special: The Easy Way to Implement](https://www.textcontrol.com/blog/2009/09/07/paste-special-the-easy-way-to-implement/llms.txt)
- [How to Remove All Section Breaks in a Document?](https://www.textcontrol.com/blog/2009/05/14/how-to-remove-all-section-breaks-in-a-document/llms.txt)
- [Batch Printing: How to Print Documents in One Print Job](https://www.textcontrol.com/blog/2009/03/20/batch-printing-how-to-print-documents-in-one-print-job/llms.txt)
- [Removing TextFields in a Loop](https://www.textcontrol.com/blog/2009/03/12/removing-textfields-in-a-loop/llms.txt)
- [Creating a Table of Contents (TOC) Using TX Text Control](https://www.textcontrol.com/blog/2009/02/02/creating-a-table-of-contents-toc-using-tx-text-control/llms.txt)
- [Rendering TX Text Control's Content to a DataGridViewCell](https://www.textcontrol.com/blog/2009/01/20/rendering-tx-text-controls-content-to-a-datagridviewcell/llms.txt)
- [Revised Sample: Converting Text to a Table](https://www.textcontrol.com/blog/2009/01/07/revised-sample-converting-text-to-a-table/llms.txt)
- [New Sample: Printing to Different Paper Trays](https://www.textcontrol.com/blog/2008/10/08/new-sample-printing-to-different-paper-trays/llms.txt)
- [TX Text Control .NET for Windows Forms Help and Support Center](https://www.textcontrol.com/blog/2008/09/30/tx-text-control-net-for-windows-forms-help-and-support-center/llms.txt)
- [Is the Current Input Position Currently Visible?](https://www.textcontrol.com/blog/2008/09/04/is-the-current-input-position-currently-visible/llms.txt)
- [New Sample: Processing MS Word Compatible Page Number Fields](https://www.textcontrol.com/blog/2008/06/25/new-sample-processing-ms-word-compatible-page-number-fields/llms.txt)
- [The ParagraphStyle and InlineStyle Constructor Unreveled](https://www.textcontrol.com/blog/2008/06/24/the-paragraphstyle-and-inlinestyle-constructor-unreveled/llms.txt)
- [Searching with C# Escape Sequences](https://www.textcontrol.com/blog/2008/05/29/searching-with-c-escape-sequences/llms.txt)
- [Removing Empty Tables](https://www.textcontrol.com/blog/2008/05/27/removing-empty-tables/llms.txt)
- [Manipulating the Selection Object](https://www.textcontrol.com/blog/2008/05/26/manipulating-the-selection-object/llms.txt)
- [Casting TextField Derivatives](https://www.textcontrol.com/blog/2008/05/23/casting-textfield-derivatives/llms.txt)
- [Printing into a PrintPreview Control](https://www.textcontrol.com/blog/2007/08/10/printing-into-a-printpreview-control/llms.txt)
- [Drag And Drop Images into TX Text Control .NET for Windows Forms](https://www.textcontrol.com/blog/2007/08/09/drag-and-drop-images-into-tx-text-control-net-for-windows-forms/llms.txt)
- [New Sample: Restarting a Numbered List](https://www.textcontrol.com/blog/2007/08/08/new-sample-restarting-a-numbered-list/llms.txt)
- [Is There a Table at the Current Mouse Position?](https://www.textcontrol.com/blog/2007/06/22/is-there-a-table-at-the-current-mouse-position/llms.txt)
- [Apply a Boxed Frame Style to Selected Cells](https://www.textcontrol.com/blog/2007/06/12/apply-a-boxed-frame-style-to-selected-cells/llms.txt)
