首先要将PictureBox中的SizeMode设置成StretchImage
然后通过更改PictureBox中的宽度和高度来实现放大和缩小的功能
//newRatio是放大缩小的倍数,缩小的话就是0.5之类的// currentImgBox此为pictureboxif (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);
}
}

本文介绍如何在C#中通过改变PictureBox组件的宽度和高度来实现图片的放大和缩小功能,包括设置SizeMode属性为StretchImage以适配图片尺寸,并通过输入倍数值来动态调整图片大小。

2030

被折叠的 条评论
为什么被折叠?



