I have a panel control with a picture box in it. How can I maintain the aspect ratio of the panel control when Resizing the form it's on?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You'll need to store off the aspect ratio somehow, whether it's something known to you at design time or if you just want to calculate it in the constructor of the form after InitializeComponent()
. In your form's Resize
event, you'll just need to set the size of the Panel to be the largest rectangle at that aspect ratio that fits within the ClientRectangle of the form.
回答2:
Use the Anchor property, or put the control in a DockContainer and use the Dock property appropriately.
Edit: Actually that's not what you asked, is it? My recommendation would be to do this manually on the resize event of the container. You'd need to calculate the x or y and apply the aspect ratio of the picture when it was loaded.