This example shows how to draw a cropped image from a file. In this instance we're going to discard a 20 pixel border round the edge of the image.


Setting Up    

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

inpath = Server.MapPath("birds.jpg")
outpath = Server.MapPath("croppedbirds.jpg")


Creating the Objects    

We will need a few objects so we create them all here for convenience.

Set gr = Server.CreateObject("ImageGlue7.Graphic")
Set rc = Server.CreateObject("ImageGlue7.XRect")
Set ca = Server.CreateObject("ImageGlue7.Canvas")


Finding the Portion of the Image to Draw    

We need to find the portion of the image we want to draw. To do this we get the natural Rectangle of the main image using the Graphic object. We then inset the Rectangle by 20 pixels on each side.

gr.SetFile inpath
If gr.Drawable Then
  rc.String = gr(1).Rectangle
  rc.Top = rc.Top + 20
  rc.Left = rc.Left + 20
  rc.Bottom = rc.Bottom - 20
  rc.Right = rc.Right - 20


Drawing the Image    

The default size of the Canvas is the natural, un-cropped size of the first image drawn on it. We are going to crop the image so we have to manually set the Canvas size to take this into account. We also use the Pos parameter to shift the image up and left to cater for our border width.

  ca.Height = rc.Height
  ca.Width = rc.Width
  ca.DrawFile inpath, "Pos=-20,-20 SrcRect=" & rc.String



Finally we save the picture as a JPEG image.

  ca.SaveAs outpath , ""
End If


Input and Output    

Sample input and output images are shown below.