This example shows how to matte one image onto another using an alpha channel. It also demonstrates a number of other useful techniques.


Setting Up    

We start by setting up variables pointing to the files we are going to use.

imagefile = Server.MapPath("suncolor.gif")
maskfile = Server.MapPath("sunmask.gif")
backgroundfile = Server.MapPath("mapusa.jpg")
dstfile = Server.MapPath("mapusasun.jpg")


Creating the Background    

We create a Canvas object and draw our background onto it. Because neither the height nor width of the Canvas was defined the Canvas is resized to the natural size of the image.

Set canvas = Server.CreateObject("ImageGlue7.Canvas")
canvas.DrawFile backgroundfile, ""


Creating the Foreground Image    

Next we create a Canvas for the image we are going to draw onto our background. There are two parts for this image - the visible part and the alpha channel which defines a mask for the image. We draw each of these pictures onto a Canvas and then insert the alpha channel into our visible image. Finally we Clear the mask seeing as we are not going to be using it again and it is occupying memory which could be more valuably utilized.

Set src = Server.CreateObject("ImageGlue7.Canvas")
Set mask = Server.CreateObject("ImageGlue7.Canvas")
src.DrawFile imagefile, ""
mask.DrawFile maskfile, ""
src.SetChannel "alpha", mask.Image


Drawing the Images    

We are going to draw five images matted into the background. To do this we have to draw using transparent mode. The first image is offset 30 pixels down and 30 to the right from the top left. Because DrawCanvas returns the rectangle used for drawing each image will be drawn offset from the last one. We end up with a diagonal line of images stretching down from the top left of the Canvas.

params = "Mode=transparent Move=30,30 Rect="
rect = "0,0," & src.Width & "," & src.Height
For i = 1 To 5
  rect = canvas.DrawCanvas(src.Image, params & rect)



Finally we save the Canvas out as a file.

canvas.SaveAs dstfile, ""


Example Images    

These are the kinds of input image you might use and the kind of output image you might expect.

imagemask.bmp image.bmp