Sample code for resizing an image using ASP

Here are two downloadable examples of resizing images in ASP using the csImageFile component. You will need to download the component and have it registered on your server. You will also need the sample files, available below.

Click here for more details of the csImageFile component.

Download the trial csImageFile component - csift.zip (4.3 MB)

Files to resize an image - sizebox.zip (2 KB)

Files to display and resize a folder of images - thumbdir.zip (4 KB)

The sample files are listed below. Both examples contain a readme.txt file contains a description of what each file does and how to make the demo work.

The SizeBox Example

This is a simple example and it shows how to use the ResizeFit function. Often an image must be resized to fit into an area bounded by a maximum height and width and ResizeFit will do this in a single command.

  MaxWidth = 320
  MaxHeight = 300
  Image.ResizeFit MaxWidth, MaxHeight

Adjust the value of MaxHeight and MaxWidth to define a different size box for the image to fit into. If the image is already smaller than MaxWidth x MaxHeight it is left unchanged. Note that the output of this script is a binary data stream and you cannot add HTML output to this script. In a working application this script would be called from an <img> tag. The next example shows an example of this.

The ThumbDir Example

This is a more complicated example but it contains some features that are worth looking at. It displays all the JPG images in a directory and arranges them in a table at a reduced size. It positions this thumbnail image centrally on a plain coloured background.

There are two scripts. One is the web page that consists of HTML and acts as a placeholder for the images. The other is the script that uses csImageFile to generate the images and produces binary output. The variables at the start of the first script can be adjusted to produce a different size or shape table or a different size thumbnail. You must set the directory path to something relevant to your system or the example will not work. The Read Me file supplied with the example contains an explanation and instructions.

One of the key features here is the way the variables are passed from the web page script to the image script by using the URL string. This is an important technique when using a component like csImageFile that generates an image.

There are some working exmples in our live demonstration area.

Full list of available ASP demos and tutorials.