This example shows how to manually make a number of thumbnail images from an original. It shows some optimizations that you might want to use and also how you can manually set the size of your Canvas.


Setting Up    

We start by setting up variables we will use later - the path to our source file and output files.

inpath = Server.MapPath("pic.jpg")
outpath1 = Server.MapPath("large.jpg")
outpath2 = Server.MapPath("medium.jpg")
outpath3 = Server.MapPath("small.jpg")


Creating the Objects    

We will need a few objects so we create them all here for convenience. While you could use only one Canvas object, using three allows you to optimize drawing speed with little overhead.

Set canvas1 = Server.CreateObject("ImageGlue7.Canvas")
Set canvas2 = Server.CreateObject("ImageGlue7.Canvas")
Set canvas3 = Server.CreateObject("ImageGlue7.Canvas")
Set graph = Server.CreateObject("ImageGlue7.Graphic")
Set rect = Server.CreateObject("ImageGlue7.XRect")


Finding the Size of the Image    

We need to find the natural size of the image that we're going to be drawing so that we can set the sizes of our thumbnails appropriately. To do this we get the natural Rectangle of the main image using the Graphic object.

graph.SetFile inpath
rect.String = graph(1).Rectangle


Making the Large Image    

We start off by manually setting the width and height of our first Canvas. The width of the Canvas will be 240 pixels (this is the width our our large thumbnail) and we scale the height so that the image fits nicely into the Canvas without getting stretched. We draw the image precisely into the Canvas and then save it out.

canvas1.Width = 240
canvas1.Height = (240 * rect.Height) / rect.Width
canvas1.DrawFile inpath, "size=" & canvas1.Width & "," & canvas1.Height
canvas1.SaveAs outpath1, ""


Making the Medium Image    

We use the same method to set the width and height of our second thumbnail. However rather than drawing the image from the file again we use the DrawCanvas method to draw canvas1.

There are a number of reasons why this technique is faster than drawing the file again. The image is already in memory and not on the disk, it is already decompressed and it is smaller. Using a single processor PII 300 and a test image approximately 400 pixels square, canvas1.DrawFile took 156 milliseconds but canvas2.DrawCanvas took only 27!

After we've finished drawing canvas1 onto canvas2 we can dispose of it using the Clear method (this is a safe way of releasing almost all of the memory it uses immediately). We then save out the second thumbnail.

canvas2.Width = 160
canvas2.Height = (160 * rect.Height) / rect.Width
canvas2.DrawCanvas canvas1.Image, "size=" & canvas2.Width & "," & canvas2.Height
canvas2.SaveAs outpath2, ""


Making the Small Image    

Using the same technique we save the last thumbnail. This time canvas3.DrawCanvas takes only 14 milliseconds!

canvas3.Width = 80
canvas3.Height = (80 * rect.Height) / rect.Width
canvas3.DrawCanvas canvas2.Image, "size=" & canvas3.Width & "," & canvas3.Height
canvas3.SaveAs outpath3, ""


Input and Output    

Sample output images are shown below.