Canvas auto resize image

Ok, I can read the images from a string type path and load them in a Canvas window. There are images of various formats, how do you make them have the resolution of the canvas window when they are loaded?

Var f As FolderItem
Var p As Picture
f = New FolderItem(PathImmagine)
p = Picture.Open(f)
Canvas1.Backdrop = p

I have tried looking at the guide and examples, but I don’t understand anything about it. In VB6 it was enough to select the Autoredraw property that does not exist here, I would like to understand how to do it. Thanks in advance for the help.

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


Thanks you Mr. Your code is perfect. YEAH !!! :slight_smile:

if your canvas scale with the window around
make a picture property and use g.DrawPicture in canvas paint event.

The CANVAS is in a portion of the window … exactly in the upper left part. And what should I include in the PAINT event? Maybe it can be useful for something else.

something like this in the Paint Event with the use of a Picture Property “Pic”

g.DrawPicture( Pic, 0, 0, g.Width, g.Height, 0, 0, Pic.Width, Pic.Height )

useful if the canvas use anchors.
you can use this aspect ratio formula in ProportionalScale which Tom mentioned too.

if you need this canvas in many windows you could subclass it to make it more reusable.