0

Im using Win Forms to do a quick little GUI for a powershell script...

In terms of the PictureBox Control, is it possible to get the image size and position information of the Adjusted image size, within the picturebox?

Quick example: Say a picturebox control is 300x300. Im using the Zoom attribute to maintain the aspect ratio of each image that is placed in the picture box. Let's say the image is 4000 x 2500. Once that image is reduced, I'd like to know its final size and coordinates. Anything I have tried so far seems to only return the original size of the image.

BentChainRing
  • 382
  • 5
  • 14
  • See the notes and two methods here: [Translate Rectangle Position in Zoom Mode Picturebox](https://stackoverflow.com/q/53800328/7444103) and the Lens scale positon calculation [here](https://stackoverflow.com/a/56128394/7444103), for example (the `GetImageScaledRatio()` and `CanvasToImageRect()` methods, specifically. Other related tools are available in the first link). – Jimi Mar 09 '20 at 17:14

1 Answers1

0

You could calculate this yourself if the width and height of both the picturebox and the original image are known:

# size of the Picturebox the image should fit in
$boxWidth  = 300               # $pictureBox.Width
$boxHeight = 300               # $pictureBox.Height
# size of the original image
$imageWidth  = 4000            # $pictureBox.Image.Width
$imageHeight = 2500            # $pictureBox.Image.Height

# calculate the image ratio
$ratioX = [double]($boxWidth / $imageWidth)
$ratioY = [double]($boxHeight / $imageHeight)
$ratio = [Math]::Min($ratioX, $ratioY)

# calculate the resulting image size
$newWidth  = [int]($imageWidth * $ratio)
$newHeight = [int]($imageHeight * $ratio)

Result:

$newWidth  --> 300
$newHeight --> 188
Theo
  • 57,719
  • 8
  • 24
  • 41
  • @Theo...It seems to me that since the Picturebox object is managing the Zoom attribute, it should know the image reduction/enlargement size of the object it is displaying; somwhere the math was already done. If not, h,w, then possibly the percentage delta? I Appreciate your suggestion but not necessarily the answer I was looking for. Will be glad to mark it if I go with this suggestion. – BentChainRing Mar 23 '20 at 19:38
  • @BentChainRing Sure, somewhere in the background the picturebox is doing the math, but I have never found a property on the picturebox, nor on the image it displays that reveals the zoomed size or the ratio factor.. Looks like this calculation is done on the fly on every resize.. – Theo Mar 24 '20 at 10:45