Resizing images with Visual Basic for .NET and csXImage

Here are some code fragments showing how to use the ActiveX control csXImage to resize images. The code is written in VB.NET. It is assumed that you have already installed either the full or trial version of csXImage and have added a control to a form in Visual Basic. More on getting started with csXImage in VB.NET.

The ResizeImage method

The ResizeImage method will resize the image that is loaded in the control by specifying a new width and height. If both parameters are given a non zero value, the new image will have exactly those dimensions. This can lead to the image losing its aspect ratio. Sometimes it is necessary to change the aspect ratio, for example with a faxed image that has different pixel densities in the X and Y directions.

The following code produces an image that is 640 x 480 pixels regardless of its original size.

AxImageBox1.ResizeImage(640, 480)

If either the width or height parameter of the ResizeImage method is zero, the new image will be resized to the width or height that is specified and will maintain the aspect ratio.

The following code will produce an image that is 200 pixels wide and it will maintain the aspect ratio. The final height will vary depending on the original height. Note that the new image will always be 200 pixels wide even if it was smaller to start with.

AxImageBox1.ResizeImage(200, 0)

The ResizeFit method

The ResizeFit method also takes a width and height parameter but it will always maintain aspect ratio and it will never enlarge an image. The new image will always fit into the size defined by the parameters.

The following code will resize the image to fit inside a box measuring 200 pixels wide by 100 pixels high. An image with dimensions 300 x 200 will be resized to 150 x 100 but an image that is 500 x 200 will be resized to 200 x 80 pixels.

AxImageBox1.ResizeFit(200, 100)

The ScaleImage method

The ScaleImage method takes a single parameter which is the percentage scaling that will be applied. 100 would cause no change, 50 would reduce the image to half the width and half the height. 200 would double the width and height. For example:

AxImageBox1.ScaleImage(50)

When an image is resized with csXImage, any supported meta data will be preserved, including IPTC, EXIF and ICC colour profiles. This meta data can be removed if required.

The resized image will appear smoother if it is a 24 bit image. That is, if the ColorFormat property is set to cf24bit. If the resized image contains jagged edges, the Resample property could be set before the resize. This gives a smoother image but it can lose detail, especially small text. When using Resample set the FilterType property as well.

Other VB.NET demos and code fragments.

Cookies

This site uses cookies for functionality, traffic analysis and for targeted advertising. Click the Accept button to accept our Cookie Policy. The Cookie Policy page offers configuration for a reduced set of cookies for this site.