2017-03-17 31 views
-1

我有一个PictureBox面板内的图片框Dock = DockStyle.Fill是否有可能在没有水平滚动条的情况下在PictureBox中拉伸图片,并且只能在WinForms中垂直滚动条?

当我将图片分配到PictureBox时,图片拉伸水平以适应可用空间并需要垂直滚动条来滚动图像。我不需要水平滚动条就可以了。

请不要将此问题设置为重复..我已经明确提到我只需要垂直滚动条和面板的运行时调整大小选项。

注:面板尺寸可能会在运行时发生变化(所以,明显也是PictureBox的尺寸)。

请帮我一把。

回答

1

该解决方案使用了一个简单的调整大小方法,您可以随时调用它来更新其大小。

先决条件:

  • 关闭对接的图片框

  • 小组的AutoScroll属性设置为true

大小的方法:

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; 
} 
+0

感谢您的回答。我会尽力让你知道 –

+1

没问题,我建议像我一样使用面板的resize事件。 (因为你说的面板大小可能会改变) – EpicKip

+1

@BalagurunathanMarimuthu如果你保持它停靠它不会顺便工作,这段代码将'停靠'它为面板(使其最大尺寸为 – EpicKip

相关问题