[RESOLVED] Find the display size of an image in a picturebox
Is there a way to tell the DISPLAY size of an image in a picture box?
If the PictureBox.SizeMode=Zoom, the picture sometimes is not displayed as big as the picture box is, and is sometimes displayed smaller, or larger, than the actual image is.
I just want to know what size it is actually displaying as.
Thanks
Greg
Re: Find the display size of an image in a picturebox
try this:
vb Code:
dim img as bitmap = directcast(picturebox1.image.clone, bitmap)
dim width as integer = img.width
dim height as integer = img.height
Re: Find the display size of an image in a picturebox
Here's my take on it. If the shape of the original image is wider and flatter than the picture box, the zoomed width will equal the width of the picturebox. If the shape of the image is taller and narrower than that of the picturebox, the height of the zoomed image will equal the picturebox height. So you have to compare the aspect ratios (height divided by width):
Code:
Private Function GetZoomedSize(ByVal img As Image, ByVal pb As PictureBox) As Size
Dim imgAspectRatio As Double = img.Height / img.Width
Dim pbAspectRatio As Double = pb.ClientSize.Height / pb.ClientSize.Width
If imgAspectRatio > pbAspectRatio Then 'image shape is taller than pb
Return New Size(CInt(pb.ClientSize.Height / imgAspectRatio), pb.ClientSize.Height)
Else 'image shape is wider than pb
Return New Size(pb.ClientSize.Width, CInt(pb.ClientSize.Width * imgAspectRatio))
End If
End Function
BB
Re: Find the display size of an image in a picturebox
Thank you for the help.
Boops boops: That is a great idea, can't believe I didn't think of that! Thanks, it worked perfectly. :D
.paul.: Your solution returns the size of the original image, not the currently displayed size.