首先要将PictureBox中的SizeMode设置成StretchImage
然后通过更改PictureBox中的宽度和高度来实现放大和缩小的功能
//newRatio是放大缩小的倍数,缩小的话就是0.5之类的
// currentImgBox此为picturebox
if (txtMulitiple.Text.Length <= 0 || null == currentImgBox.Image)
{
return;
}
float newRatio = 0;
try
{
newRatio = float.Parse(txtMulitiple.Text);
}
catch (System.Exception)
{
MessageBox.Show(@"请输入数字");
txtMulitiple.Text = "";
return;
}
if (0.1 < newRatio && newRatio <= 16)
{
zoomRatio = newRatio;
currentImgBox.Width = (int)(currentImgBox.Image.Width * zoomRatio);
currentImgBox.Height = (int)(currentImgBox.Image.Height * zoomRatio);
if (null != currentImgBox.Image)
{
currentImgBox.Width = (int)(currentImgBox.Image.Width * zoomRatio);
currentImgBox.Height = (int)(currentImgBox.Image.Height * zoomRatio);
}
if (null != currentImgBox.Image)
{
currentImgBox.Width = (int)(currentImgBox.Image.Width * zoomRatio);
currentImgBox.Height = (int)(currentImgBox.Image.Height * zoomRatio);
}
}