VB.NET 2005 picturebox: resizing and retaining aspect ratio

Hello.  I have a VB.NET 2005 picturebox to work with.  I also have several images that vary in size.  When an image is loaded into the picturebox, I want to resize the image so that it has a width of 500, and I want to retain the aspect ratio for the height.  So, it should be resized to 500 x ? and be fully displayed in a form (not clipped and not stretched).  How do I go about this?
adwooley2Asked:
Who is Participating?
 
Wayne Taylor (webtubbs)Commented:
Hello adwooley2,

try something like this....

        Dim img As Image = Image.FromFile("C:\YourFile.JPG")
        PictureBox1.Height = img.Height / (img.Width / 500)
        PictureBox1.Width = 500
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
        PictureBox1.Image = img

Regards,

Wayne
0
 
adwooley2Author Commented:
Well, I adapted what you had.  Thanks for your simple solution!

Regards,
Alan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.