Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
« »

Resize images to fit into page

Blogged by Björn Meyer on May 08, 2008 and tagged with .NET, Samples, Images.

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.

The TX Text Control video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

« Prev:BrowserTextControl: Using post-build events in Visual Studio
» Next:Digital Signatures - Code Signing the TX Text Control files

top

Top 10 Bestselling Product Award 2007Top 25 Publisher Product Award 2007Top 10 Bestselling Product Award 2007 in JapanTop 25 Bestselling Product Award 2006Top 25 Bestselling Publisher Award 2006Reader's Choice Award, dot.net magazin, 3rd place