Results 1 to 4 of 4

Thread: [RESOLVED] Find the display size of an image in a picturebox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    268

    Resolved [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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Find the display size of an image in a picturebox

    try this:

    vb Code:
    1. dim img as bitmap = directcast(picturebox1.image.clone, bitmap)
    2. dim width as integer = img.width
    3. dim height as integer = img.height

  3. #3
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    268

    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.

    .paul.: Your solution returns the size of the original image, not the currently displayed size.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width