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. We assume the image is bigger than 40x40 pixels.
Creating the Canvas

We will need a canvas so we create it here.

[C#] Canvas canvas = new Canvas();

[Visual Basic] Dim canvas As New Canvas()

Creating the image

We create an image and find the portion of the image we want to draw. To do this we get the image Bounding Box. We then shrink this rectangle by 20 pixels on each side and set it as the image selection in the draw options.

[C#] XImage image = XImage.FromFile(Server.MapPath("rez/birds.jpg"));
DrawOptions drawOpts = new DrawOptions();
drawOpts.Selection = image.BoundingBox;
drawOpts.Selection.Shrink(20, 20);

[Visual Basic] Dim image As XImage = XImage.FromFile(Server.MapPath("rez/birds.jpg"))
Dim drawOpts As New DrawOptions()
drawOpts.Selection = image.BoundingBox
drawOpts.Selection.Shrink(20, 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. Then we draw the image onto the Canvas. We also specify the image position to shift the image up and left to cater for our border width.

[C#] canvas.Width = drawOpts.Selection.Width;
canvas.Height = drawOpts.Selection.Height;
canvas.DrawImage(image, new XPoint(-20, -20), drawOpts);

[Visual Basic] canvas.Width = drawOpts.Selection.Width
canvas.Height = drawOpts.Selection.Height
canvas.DrawImage(image, New XPoint(-20, -20), drawOpts)


Finally we save the picture as a JPEG image.

[C#] canvas.SaveAs(Server.MapPath("Cropping_an_Image_4.jpg"));

[Visual Basic] canvas.SaveAs(Server.MapPath("Cropping_an_Image_4.jpg"))

Input and Output

Sample input and output images are shown below.