I have the following code that works fine but always places the image in the top left corner.
// Calculate the scale ratio
Dim ratio As Double = min(maxHeight / p.height, maxWidth / p.width)
// Create a new picture to return
Dim newPic As New Picture(p.width * ratio, p.height * ratio)
// Draw picture in the new size
newPic.graphics.DrawPicture(p, 0, 0, newPic.width, newPic.height, 0, 0, p.width, p.height)
I tried changing to
// Draw picture in the new size
dim x as double = (maxWidth-newPic.Width)/2
dim y as double = (maxHeight-newPic.Height)/2
newPic.graphics.DrawPicture(p, x, y, newPic.width, newPic.height, 0, 0, p.width, p.height)
the picture gets shifted over some (not centered) and it crops off part of the right side. What am I doing wrong? MaxHeight and MaxWidth is the size of the canvas
Instead of creating a new picture and assigning it as the canvas’ backdrop, draw the picture directly in the canvas Paint event.
You can then draw the picture at the desired position and size.
I have multiple images that will get drawn on the same canvas as I navigate through my database. I figured it would be best to draw on the backdrop. But this seems to work fine, not sure why my code did not…
I had considered that and added the amount of x and y margin to the edge of the app for the the x, y values as well and it didn’t seem to help any. Can you elaborate on how I should have done it, for future reference.
Draw to newPic with no offset, as you did originally. Then calculate the appropriate xy values in the Paint event and apply them there with no additional scaling.