比如滚轮向前推是缩小,向后推是放大,类似这样的。
是picturebox里图片的放大的缩小,不是控件放大和缩小
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Image sourceImage = null;
private int imageWidth = 0;
private int imageHeight = 0;
private float zoom = 1;
private const float reduce = 0.1f;
private const float enlarge = 1f;
private void Form1_Load(object sender, EventArgs e)
{
sourceImage = ZoomImage.Properties.Resources._123456;
imageWidth = sourceImage.Width;
imageHeight = sourceImage.Height;
pictureBox1.MouseWheel += pictureBox1_MouseWheel;
}
private
void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
if (e.Delta > 0)
{
if (zoom > 1)
{
zoom -= enlarge;
}
else
{
zoom -= reduce;
}
if (zoom >= 10)
{
zoom = 10;
}
if (zoom <= 0)
{
zoom = 0.1f;
}
}
else
{
if (zoom > 1)
{
zoom += enlarge;
}
else
{
zoom += reduce;
}
if (zoom >= 10)
{
zoom = 10;
}
if (zoom <= 0)
{
zoom = 0.1f;
}
}
pictureBox1.Image = ZoomImageMethod((Image)ZoomImage.Properties.Resources._123456, Convert.ToInt32(Math.Ceiling(imageWidth * zoom)), Convert.ToInt32(Math.Ceiling(imageHeight * zoom)));
}
private Image ZoomImageMethod(Image bitmap, int destHeight, int destWidth)
{
try
{
System.Drawing.Image sourImage = bitmap;
int width = 0, height = 0;
//按比例缩放
int sourWidth = sourImage.Width;
int sourHeight = sourImage.Height;
if (sourHeight > destHeight || sourWidth > destWidth)
{
if (sourHeight / (destHeight * 1.0) > sourWidth / (destWidth * 1.0))
{
height = destHeight;
width = Convert.ToInt32(Math.Floor(sourWidth * destHeight / (sourHeight * 1.0)));
}
else
{
width = destWidth;
height = Convert.ToInt32(Math.Floor(sourHeight * destWidth / (sourWidth * 1.0)));
}
}
else
{
width = sourWidth;
height = sourHeight;
}
Bitmap destBitmap = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage(destBitmap);
g.Clear(Color.White);
//设置画布的描绘质量
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(sourImage, new Rectangle((destWidth - width) / 2, (destHeight - height) / 2, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel);
g.Dispose();
//设置压缩质量
System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
sourImage.Dispose();
return destBitmap;
}
catch
{
return bitmap;
}
}
}
图片不能修改大小,控件可以修改大小。
要么你在picturebox外面套个panel,把图片设置为铺满,然后修改picturebox大小,感觉像是在修改图片大小一样
要么你重绘picturebox控件