简体   繁体   English

如何在图片框上获取滚动条

[英]How can I get scrollbars on Picturebox

I have PictureBox picture .我有PictureBox picture

I use:我使用:

picture.Size = bmp.Size;
picture.Image = bmp;

Let's say there are two integers maxWidth and maxHeigth .假设有两个整数maxWidthmaxHeigth
I want to add vertical/horizontal scrollbar to picture when its size exceeds maxWidth and/or maxHeight .picture大小超过maxWidth和/或maxHeight时,我想向图片添加垂直/水平滚动条。 How can I do that?我怎样才能做到这一点?

You can easily do it with a Panel Control您可以使用Panel控件轻松完成

Insert a panel to your form, say panel1 and set在表单中插入一个面板,比如 panel1 并设置

panel1.AutoScroll = true;

insert a PictureBox to the Panel , say picture and set插入一个PictureBoxPanel ,说图片并设置

picture.SizeMode = PictureBoxSizeMode.AutoSize;

and set the Image并设置图像

picture.Image = bmp;

hope this helps希望这可以帮助

Here's a project where a guy built an ImagePanel user control that you can drop onto a form;这是一个项目,其中一个人构建了一个ImagePanel用户控件,您可以将其拖放到表单上; it gives you scrollbars and zoom capability.它为您提供滚动条和缩放功能。

http://www.codeproject.com/KB/graphics/YLScsImagePanel.aspx http://www.codeproject.com/KB/graphics/YLScsImagePanel.aspx

I got it to work by also putting a picturebox inside a panel control, I set the Panel's AutoScroll property to true, but I also set the Panel's Autosize property to True, and the Panel's Dock property to Fill (that way when the user resizes the form - so will the Panel).我通过在面板控件中放置一个图片框来让它工作,我将面板的 AutoScroll 属性设置为 true,但我也将面板的 Autosize 属性设置为 True,将面板的 Dock 属性设置为 Fill(这样当用户调整表格 - 小组也是如此)。 For the Picturebox, I set it's Dock property to None, and the SizeMode to Autosize (so it resizes also when the Panel and form Resizes. It worked like a charm, the Picturebox has Scrollbars and when the user resizes the form - everything is still placed correctly!对于图片框,我将它的 Dock 属性设置为无,并将 SizeMode 设置为自动大小(因此它也会在面板和表单调整大小时调整大小。它就像一个魅力,图片框有滚动条,当用户调整表单大小时 - 一切都还在正确放置!

Another suggestion is to put the picturebox inside a FlowlayoutPanel .另一个建议是将图片框放在 FlowlayoutPanel 中。

Set the Auto scroll of the FlowlayoutPanel to true and set the picture size mode to normal FlowlayoutPanel的Auto scroll设置为true,图片大小模式设置为normal

Using a FlowlayoutPanel makes sure the image is always at 0,0 in the panel使用 FlowlayoutPanel 确保面板中的图像始终位于 0,0

It works to me.它对我有用。

PictureBox picture = new PictureBox();
picture.Image=Image.FromFile("image.bmp");
picture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
Panel panel = new Panel();
panel.Size=new Size(800,600);
panel.Location=new Point(0,0);
panel.AutoScroll=true;
panel.Controls.Add(picture);
this.Controls.Add(panel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM