Is it possible to stretch picture in PictureBox without horizontal scrollbar and only Vertical scrollbar in WinForms?

廉价感情. 提交于 2019-12-12 07:05:06

问题


I have a PictureBox inside the Panel with picture box Dock = DockStyle.Fill.

When I assign picture into the PictureBox, picture is stretch horizontal to fit the space available and need a vertical scrollbar to scroll the image. I do not need of horizontal scrollbar in it.

Please do not make this question as duplicate.. I have clearly mentioned that I only need of vertical scrollbar with run time resizing option of the panel.

Note: Panel size may change at run time (so, obviously PictureBox size too).

Please help me out.


回答1:


This solution uses a simple resize method which you can call whenever it should update its size.

prerequisites:

  • Turn off the docking for the PictureBox

  • Set the AutoScroll property of the Panel to true

Resizing method:

private void panel1_Resize(object sender, EventArgs e)
{
    ResizePb();
}

private void ResizePb()
{
    //Make the pciturebox as wide as the panel, keep in mind the scrollbars
    pictureBox1.Width = panel1.Width - SystemInformation.VerticalScrollBarWidth - 5; 
    //Calculate the aspect ratio of the image based on its new width
    var aspect = (double)panel1.Width / pictureBox1.Image.Width;
    //Set height according to aspect ratio
    var height = Convert.ToInt32(aspect * pictureBox1.Image.Height);
    pictureBox1.Height = height;
}


来源:https://stackoverflow.com/questions/42857528/is-it-possible-to-stretch-picture-in-picturebox-without-horizontal-scrollbar-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!