Hello,
to center an image in a canvas I use this method:
[code]Public Sub Picture_Scale(g as Graphics, the_Picture as Picture, the_Padding as Integer)
if the_Picture = nil then
// log(CurrentMethodName, “the_Picture = nil”)
Return
end if
dim Ratio as Double
dim xDisplacement as Integer
dim yDisplacement as Integer
dim CanvasAspectRatio as Double
dim PictureAspectRatio as Double
dim gAvailableWidth as Integer
dim gAvailableHeight as Integer
gAvailableWidth = g.Width - (the_Padding * 2)
gAvailableHeight = g.Height - (the_Padding * 2)
CanvasAspectRatio = (gAvailableWidth / gAvailableHeight)
PictureAspectRatio = the_Picture.Width / the_Picture.Height
Ratio = Min(gAvailableWidth / the_Picture.Width, gAvailableHeight / the_Picture.Height)
if CanvasAspectRatio <= PictureAspectRatio then
yDisplacement = Floor((g.Height * 0.5) - (the_Picture.Height * Ratio * 0.5))
g.DrawPicture(the_Picture, the_Padding, yDisplacement, the_Picture.Width * Ratio, the_Picture.Height * Ratio, 0, 0, the_Picture.Width, the_Picture.Height)
else
xDisplacement = Floor((g.Width * 0.5) - (the_Picture.Width * Ratio * 0.5))
g.DrawPicture(the_Picture, xDisplacement, the_Padding, the_Picture.Width * Ratio, the_Picture.Height * Ratio, 0, 0, the_Picture.Width, the_Picture.Height)
end if
End Sub[/code]
In the Paint-event of the canvas I call the method like this:
Picture_Scale(g, thePicture, 2)