有没有一种简单实用的方法可以在picturebox中包括滚动条缩放图片?
目前,我在面板中使用带自动滚动的picture box。为了进行缩放,我会放大picturebox并使用面板上的滚动条移动它。问题是,它的行为很奇怪。例如,如果你缩放太远,上部和左侧表单边框与图像之间的边距会越来越大。
这就是缩放方法。我从这里获取了它。
private void ZoomInOut(bool zoom)
{
//Zoom ratio by which the images will be zoomed by default
int zoomRatio = 10;
//Set the zoomed width and height
int widthZoom = pictureBox_viewer.Width * zoomRatio / 100;
int heightZoom = pictureBox_viewer.Height * zoomRatio / 100;
//zoom = true --> zoom in
//zoom = false --> zoom out
if (!zoom)
{
widthZoom *= -1;
heightZoom *= -1;
}
//Add the width and height to the picture box dimensions
pictureBox_viewer.Width += widthZoom;
pictureBox_viewer.Height += heightZoom;
}
非常感谢任何帮助。
提前致谢。
马可
编辑: 一张未放大和一张放大了16倍的图像的两个截屏。
请注意图像上边框与表单上边框之间的空白边距。
PictureBox
、Panel
的其他事件的代码。 - King Kingprivate void Form1_Resize(object sender, EventArgs e) {pictureBox_viewer.Size = panel_viewer.Size; if (viewerSize != Size.Empty) {viewerSize = pictureBox_viewer.Size;} }
它是用于在调整窗体大小时将pictureBox适配到panel的。 - Marco Frost