c#把picturebox放大
时间: 2023-11-24 22:48:10 浏览: 54
要实现C#中PictureBox的放大功能,可以通过以下步骤实现:
1.在PictureBox的MouseDown事件中记录鼠标按下时的坐标。
2.在PictureBox的MouseMove事件中计算鼠标移动的距离,并根据距离计算出PictureBox的新大小。
3.在PictureBox的MouseUp事件中更新PictureBox的大小。
4.在PictureBox的Paint事件中绘制图片。
具体实现代码如下:
private Point mouseDownPoint;
private bool isResizing = false;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
mouseDownPoint = e.Location;
isResizing = true;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isResizing)
{
int width = pictureBox1.Width + (e.X - mouseDownPoint.X);
int height = pictureBox1.Height + (e.Y - mouseDownPoint.Y);
pictureBox1.Size = new Size(width, height);
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isResizing = false;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(pictureBox1.Image, 0, 0, pictureBox1.Width, pictureBox1.Height);
}