Preserve Aspect Ratio When Resizing Form

M

Marcus Kwok

I have a Form that has a PictureBox in it that has a dock style set to
"fill". I can set the PictureBox's PictureBoxSizeMode to StretchImage,
but I would like to preserve the aspect ratio of the image so that the
user may scale the size of the image but not squish it out of
proportion. Is there a simple way to do this?

If it is not possible or requires too much overhead I may just use the
ScrollablePictureBox from
http://www.c-sharpcorner.com/Graphics/ScrollablePictureBox.asp
and forget about being able to resize the image.

I am writing in Managed C++ (.NET 1.1) but in the past I have not had
much difficulty translating code from VB.NET or C# into Managed C++.

Thanks for any advice.
 
L

larrylard

Marcus said:
I have a Form that has a PictureBox in it that has a dock style set to
"fill". I can set the PictureBox's PictureBoxSizeMode to StretchImage,
but I would like to preserve the aspect ratio of the image so that the
user may scale the size of the image but not squish it out of
proportion. Is there a simple way to do this?

If it is not possible or requires too much overhead I may just use the
ScrollablePictureBox from
http://www.c-sharpcorner.com/Graphics/ScrollablePictureBox.asp
and forget about being able to resize the image.

I am writing in Managed C++ (.NET 1.1) but in the past I have not had
much difficulty translating code from VB.NET or C# into Managed C++.

The example code at

<http://tinyurl.com/mc9wn>
=
<http://msdn.microsoft.com/library/e...SystemWindowsFormsControlClassResizeTopic.asp>

should point you in the right direction.
 
M

Marcus Kwok


Thanks for the link. Keeping the window square is easy and works.
However, I am having problems with my method:

I added a double to the class called aspect_ratio.

In the constructor, I calculate

aspect_ratio = static_cast<double>(image->Width) / static_cast<double>(image->Height);


Then in the Resize event handler:

System::Void PlotDisplayWindow_Resize(System::Object * sender, System::EventArgs * e)
{
Control* control = __try_cast<Control*>(sender);

// Looking at this, I think I need Height instead of Width, but the
// concept is what's important
control->Size = System::Drawing::Size(static_cast<int>(aspect_ratio * control->Size.Width), control->Size.Width);
}


When I do this and try to resize the form, I get a flickering of
rectangles of the appropriate aspect ratio as I drag the border of the
frame, but as soon as I let go of the border then the window snaps back
to its original size.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top