Canvas auto resize image

Below is a variant of the code found in this thread: Is there a simple way (such as property/method) to scale image in WebImageView?. Often times if you search this forum for what you are looking for you will find it.

Here is your code adapted to use the Function below:

Var f As FolderItem
f = New FolderItem(PicturePath, FolderItem.PathModes.Native)
Var p1 As Picture
p1 = Picture.Open(f)
Var p2 As Picture = ProportionalScale(p1,Canvas1.Width,Canvas1.Height)
Canvas1.Backdrop = p2

Function adapted from the thread:

Public Function ProportionalScale(Pic as Picture, Width as integer, Height as Integer) As Picture

  // calculate the scale factor
  dim factor as Double = min( Height / Pic.Height, Width / Pic.Width )
  
  // Calculate new size
  dim w as integer = Pic.Width * factor
  dim h as integer = Pic.Height * factor
  
  // create new picture
  dim NewPic as new Picture( w, h, 32 )
  
  // draw picture in the new size
  NewPic.Graphics.DrawPicture( Pic, 0, 0, w, h, 0, 0, Pic.Width, Pic.Height )
  
  // return scaled image
  Return NewPic

End Function

4 Likes