How to resize a picture box image while keeping the aspect ratio?

I was wondering how I could write code to resize a picture box image that will allow the image to scale with the size of the form without stretching beyond its normal aspect ratio. In other words, I don't want it looking squashed.

I already have code that handles resizing the picture box control as the form is resized, but at present I simply have the image in the box remain unchanged, which results in parts of large images not being visible in the picture box. I want to do this so I can scale those big images down properly to fit the current window size.

I'm asking this because I've been fiddling with scrollbars for two hours and have accomplished very little :) Any help would be greatly appreciated.

Comments

  • I almost have it right, but not quite... Here's what I'm working with now. I had to post it as a text file because this forum hacks my code to pieces. Make sure you have word wrap off if you view it in notepad. I also included a screenshot indicating the problem.

    http://pages.xtn.net/~jcestep/code.txt
    http://pages.xtn.net/~jcestep/images/test.jpg

    The image in the picture box is a 1280x1024 bitmap. My desktop res is the same, so the image would be considerably larger than the picture box initially. The image has a three pixel thick line around the outer edge. As you can see, it is visible at the top and bottom edge, but cropped off on the left and right. Also, if I reduce the size of the form far enough, it chops off the bottom of the image.

    pbImage is a picture box, of course. The first two lines of code resize pbImage so that it fills all of the free space in the form (the width of the form minus the width of my treeview control). The last line repositions the left edge of pbImage against the treeview control.

    DecSrcRatio is the aspect ratio of the original, unmodified image file. DecDstRatio is the aspect ratio of the picture box itself.

    This routine is called whenever I load a new image and whenever the user resizes the form. And the point of it all is to simply resize the image to fill as much of that empty space in the form without distorting it or stretching it horizontally or vertically.

    Any advice at all would be greatly appreciated.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion