From Xojo Documentation

Revision as of 00:51, 17 January 2022 by Gperlman (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

Graphics.DrawPicture(Image As Picture, x As Double, y As Double [, destWidth As Double] [, destHeight As Double] [, sourceX As Double] [, sourceY As Double] [, sourceWidth As Double ] [, sourceHeight As Double])

Supported for all project types and targets.

Draws the picture at the specified location. The picture can be shown at full size, cropped, or scaled. All units are pixels.


X and Y are the distances from the top-left corner of the control or window from which the 0,0 point of the image will be drawn. The optional parameters are used to copy a portion of the picture (cropping) and for scaling the picture.

If you are cropping, then DestWidth and DestHeight are also required. If you are doing scaling, then all of the optional parameters are required. You scale an image by making the destination width and height different from the source width and height.

The parameters SourceX, SourceY, SourceWidth, and SourceHeight describe the portion of the image that will be scaled. DestWidth and DestHeight are used to change the scaling of the picture when SourceWidth and SourceHeight are provided. SourceX and SourceY default to 0 and are used to determine the upper-left coordinate you wish to copy from.

Negative values for DestWidth, DestHeight, SourceWidth and SourceHeight are not supported so the behavior is undefined.

Sample Code

The following example scales an image by 50%. The image, MyImage, has been added to the project. Its size is 600 x 915. Note that the destination width and height are half the original width and height. The code is in the Paint event of a Canvas. A call to DrawRect adds a black border around the Canvas.

g.DrawingColor = &c000000
g.DrawRectangle(0, 0, Me.Width, Me.Height)
Var scale As Double = 0.50
g.DrawPicture(MyImage, 0, 0, MyImage.Width * scale, MyImage.Height * scale, 0, 0, MyImage.Width, MyImage.Height)

The following code crops the image. It copies only the top 150 pixels of the original image at full size:

g.DrawPicture(MyImage, 0, 0, 600, 150)

The following code crops and scales the image. The top half of the image is retained and the DestWidth and DestHeight parameters specify a 50% reduction.

g.DrawPicture(MyImage, 0, 0, 300, 228, 0, 0, 600, 457)

If you don't want to scale or crop the image, you can leave off the last six parameters. In this case, it will appear full-size.

g.DrawPicture(MyImage, 0, 0)