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.

 

   
1
 
Setting Up    
     

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

[Visual Basic]
Dim inpath, outpath1, outpath2, outpath3 As String
inpath = Server.MapPath("rez/pic.jpg")
outpath1 = Server.MapPath("large.jpg")
outpath2 = Server.MapPath("medium.jpg")
outpath3 = Server.MapPath("small.jpg")

[C#]
string inpath = Server.MapPath("rez/pic.jpg");
string outpath1 = Server.MapPath("large.jpg");
string outpath2 = Server.MapPath("medium.jpg");
string outpath3 = Server.MapPath("small.jpg");

 

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

[Visual Basic]
Dim canvas1 As New Canvas
Dim canvas2 As New Canvas
Dim canvas3 As New Canvas
Dim graph As New Graphic
Dim rect As New XRect

[C#]
Canvas canvas1 = new Canvas();
Canvas canvas2 = new Canvas();
Canvas canvas3 = new Canvas();
Graphic graph = new Graphic();
XRect rect = new XRect();

 

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

[Visual Basic]
graph.SetFile(inpath)
rect.String = graph(0).Rectangle

[C#]
graph.SetFile(inpath);
rect.String = graph[0].Rectangle;

 

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

[Visual Basic]
canvas1.Width = 240
canvas1.Height = (240 * rect.Height) / rect.Width
canvas1.DrawFile(inpath, "size=" + canvas1.Width.ToString() + "," + canvas1.Height.ToString())
canvas1.SaveAs(outpath1, "")

[C#]
canvas1.Width = 240;
canvas1.Height = (240 * rect.Height) / rect.Width;
canvas1.DrawFile(inpath, "size=" + canvas1.Width.ToString() + "," + canvas1.Height.ToString());
canvas1.SaveAs(outpath1, "");

 

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

[Visual Basic]
canvas2.Width = 160
canvas2.Height = (160 * rect.Height) / rect.Width
canvas2.DrawCanvas(canvas1.Image, "size=" + canvas2.Width.ToString() + "," + canvas2.Height.ToString())
canvas1.Clear()
canvas2.SaveAs(outpath2, "")

[C#]
canvas2.Width = 160;
canvas2.Height = (160 * rect.Height) / rect.Width;
canvas2.DrawCanvas(canvas1.Image, "size=" + canvas2.Width.ToString() + "," + canvas2.Height.ToString());
canvas1.Clear();
canvas2.SaveAs(outpath2, "");

 

   
6
 
Making the Small Image    
     

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

[Visual Basic]
canvas3.Width = 80
canvas3.Height = (80 * rect.Height) / rect.Width
canvas3.DrawCanvas(canvas2.Image, "size=" + canvas3.Width.ToString() + "," + canvas3.Height.ToString())
canvas2.Clear()
canvas3.SaveAs(outpath3, "")
canvas3.Clear()

[C#]
canvas3.Width = 80;
canvas3.Height = (80 * rect.Height) / rect.Width;
canvas3.DrawCanvas(canvas2.Image, "size=" + canvas3.Width.ToString() + "," +canvas3.Height.ToString());
canvas2.Clear();
canvas3.SaveAs(outpath3, "");
canvas3.Clear();

 

   
7
 
Input and Output    
     

Sample output images are shown below.

large.jpg

medium.jpg

small.jpg