This example shows how to draw an image from a file, put a frame round it and then draw some rotated text at the bottom right of the image.
Creating the Canvas

Create the Canvas using the default constructor. Set the text font, size and color. Fix the Canvas Width to 300 pixels.

[C#] Canvas canvas = new Canvas();
canvas.TextFont = "Arial";
canvas.TextSize = 7;
canvas.TextColor = new XColor(System.Drawing.Color.Gray);
canvas.Width = 300;

[Visual Basic] Dim canvas As New Canvas()
canvas.TextFont = "Arial"
canvas.TextSize = 7
canvas.TextColor = New XColor(System.Drawing.Color.Gray)
canvas.Width = 300

Drawing the Image

We create an XImage object. This object will load the image and will provide us with image information such as width and height. We also create a DrawOptions object, which contains options for drawing on the Canvas. If you specify a canvas in the DrawOptions constructor, its properties will be initialized from the equivalent Canvas properties. Otherwise default values will be used. We set the ImageFit parameter in the draw options to AspectRatio. This ensures the image aspect ratio will be preserved. As no height has been specified for the Canvas, DrawImage will work one out for us. Because we have specified a width, a height will automatically be chosen that maintains the correct aspect ratio of the source image.

If the image is smaller than the Canvas, then we will want to draw it in the original size and position it using the align parameters. In this example the image will be centered horizontally and vertically. If you prefer to stretch the image than use ImageFitType.Stretch. However in this case you would want to set the size of the Canvas as well so that the image can be stretched to fit in the Canvas.

[C#] XImage image = XImage.FromFile(Server.MapPath("rez/birds.jpg"));
DrawOptions drawOpts = new DrawOptions(canvas);
drawOpts.ImageFit = DrawOptions.ImageFitType.AspectRatio;
drawOpts.VAlign = 0.5;
drawOpts.HAlign = 0.5;
canvas.DrawImage(image, drawOpts);

[Visual Basic] Dim image As XImage = XImage.FromFile(Server.MapPath("rez/birds.jpg"))
Dim drawOpts As New DrawOptions(Canvas)
drawOpts.ImageFit = DrawOptions.ImageFitType.AspectRatio
drawOpts.VAlign = 0.5
drawOpts.HAlign = 0.5
canvas.DrawImage(image, drawOpts)

Drawing a Frame

Here we draw a frame around the edge of the canvas. We just have to draw a rectangle the same size as the canvas and specify the ShapeDrawing as ShapeDrawingType.Draw. This ensures only a frame is drawn. We can reuse the same draw options we created above as the parameters that we previously set have no effect for this operation: ImageFit is not used for shapes and the align parameters are ignored when a rectangle or a position is specified. Alternatively you could have created new DrawOptions or called DrawOptions.Reset.

[C#] drawOpts.ShapeDrawing = DrawOptions.ShapeDrawingType.Draw;
canvas.DrawRect(new XRect(0, 0, image.Width, image.Height), drawOpts);

[Visual Basic] drawOpts.ShapeDrawing = DrawOptions.ShapeDrawingType.Draw
canvas.DrawRect(New XRect(0, 0, image.Width, image.Height), drawOpts)

Drawing the Text

Because we are going to draw text at the bottom right of the image we first find out how much space one line of text is going to take up and then enlarge the Canvas by this amount. We then draw the text rotated and shifted to the bottom of the Canvas.

GetTextMetrics will return an XText object. This object contains some text metrics such as the line height or text width. For convenience XText also stores the text passed into GetTextMetrics in the read only property Text.

To specify a geometric transformation you can use the Transform property in the draw options.

[C#] XText text = canvas.GetTextMetrics("Copyright John Smith 2002", drawOpts);
canvas.Width += text.LineHeight + 2;
drawOpts.Transform.Rotate(-90, 0, 0);
drawOpts.Transform.Translate(300, canvas.Height-1);
canvas.DrawText(text.Text, drawOpts);

[Visual Basic] Dim text As XText = Canvas.GetTextMetrics("Copyright John Smith 2002", drawOpts)
canvas.Width += text.LineHeight + 2
drawOpts.Transform.Rotate(-90, 0, 0)
drawOpts.Transform.Translate(300, canvas.Height - 1)
canvas.DrawText(text.Text, drawOpts)


Finally we save the picture as a high quality JPEG image.

[C#] canvas.Export.Quality = 75;

[Visual Basic] canvas.Export.Quality = 75;

Input and Output

Sample input and output images are shown below.