Type Default Value Read Only Static Description
ImageFitType ImageFitType.None No No How to fit an image on a Canvas.

This property determines if the size of the image is changed in order to fit the image on a Canvas. This property is effective only when no size has been specified for the image.

  • When Fit is None, the default, the image is simply positioned. The image natural size is used.
  • When Fit is Aspect Ratio the image is scaled to fit the Canvas but the aspect ratio is preserved.
  • When Fit is Stretch the image is stretched to cover the Canvas, even if this results in significant distortion.

To position the image you can specify the image position when drawing or else rely on automatic positioning via HAlign and VAlign.

Remember to set the Canvas size before drawing the image if you wish to change this property. Otherwise the Canvas is simply sized to the image natural size.

See Also



Here we center an image in a Canvas:

  • We simply position the image with its natural size: Fit = None.
  • We scale the image but preserve aspect ratio: Fit = AspectRatio.
  • We stretch the image on the Canvas: Fit = Stretch
  • We set the size of the image: Fit is ignored.

[C#]Canvas canvas = new Canvas(200, 200, new XColor(Color.DeepSkyBlue));
XImage image = XImage.FromFile(Server.MapPath("rez/boat.gif"));
DrawOptions drawOpts = new DrawOptions();
drawOpts.HAlign = 0.5;
drawOpts.VAlign = 0.5;
drawOpts.ImageFit = DrawOptions.ImageFitType.None;
canvas.DrawImage(image, drawOpts);
canvas.Clear(200, 200);
drawOpts.ImageFit = DrawOptions.ImageFitType.AspectRatio;
canvas.DrawImage(image, drawOpts);
canvas.Clear(200, 200);
drawOpts.ImageFit = DrawOptions.ImageFitType.Stretch;
canvas.DrawImage(image, drawOpts);
canvas.Clear(200, 200);
canvas.DrawImage(image, 50, 50, drawOpts);

[Visual Basic]Dim canvas As New Canvas(200, 200, New XColor(Color.DeepSkyBlue))
Dim image As XImage = XImage.FromFile(Server.MapPath("rez/boat.gif"))
Dim drawOpts As New DrawOptions()
drawOpts.HAlign = 0.5
drawOpts.VAlign = 0.5
drawOpts.ImageFit = DrawOptions.ImageFitType.None
canvas.DrawImage(image, drawOpts)
canvas.Clear(200, 200)
drawOpts.ImageFit = DrawOptions.ImageFitType.AspectRatio
canvas.DrawImage(image, drawOpts)
canvas.Clear(200, 200)
drawOpts.ImageFit = DrawOptions.ImageFitType.Stretch
canvas.DrawImage(image, drawOpts)
canvas.Clear(200, 200)
canvas.DrawImage(image, 50, 50, drawOpts)