C# 如何以及在何处处置旧映像?在pictureBox中将图像设置为最小和最大的大小是多少?
Im使用鼠标滚轮放大/缩小pictureBox中的图像: 在表格1的顶部:C# 如何以及在何处处置旧映像?在pictureBox中将图像设置为最小和最大的大小是多少?,c#,winforms,C#,Winforms,Im使用鼠标滚轮放大/缩小pictureBox中的图像: 在表格1的顶部: double increment = 1.25; double factor = 1.0; Image img; 在构造函数中: img = new Bitmap(@"d:\radar000075.png"); pictureBox1.Load(@"d:\radar000075.png"); 然后选择两个事件并调整mthod的大小: void pictureBox1_MouseHover(object sender,
double increment = 1.25;
double factor = 1.0;
Image img;
在构造函数中:
img = new Bitmap(@"d:\radar000075.png");
pictureBox1.Load(@"d:\radar000075.png");
然后选择两个事件并调整mthod的大小:
void pictureBox1_MouseHover(object sender, EventArgs e)
{
pictureBox1.Focus();
}
void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
if (e.Delta > 0)
factor *= increment;
else
factor /= increment;
pictureBox1.Image = resizeImage(img, new Size((int)(img.Width * factor), (int)(img.Height * factor)));
}
public static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}
现在它的工作没有任何问题,但是:
Bitmap
实现了IDisposable
接口,因此,是的,从技术上讲,您应该在使用完毕后将其处理掉。否则,这将由垃圾收集器来完成,而您永远不知道它将在何时触发。因此,如果由于每次创建的新位图而多次调整大小,您可能会看到内存消耗上下波动
对于因子限制,我个人会从pictureBox1\u MouseWheel
中提取当前因子计算代码,并将其添加到新方法中。对于事件业务而言,不需要了解调整大小,因此应该采用单独的方法(您应该查看下面的示例)。只需在已有的if中添加其他条件,就可以轻松完成该工作。可能是这样的:
void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
CalculateNewSizeFactor(e.Delta);
pictureBox1.Image = resizeImage(img, new Size((int)(img.Width * factor), (int)(img.Height * factor)));
}
private void CalculateNewSizeFactor(int delta)
{
if (delta > 0 && factor < 2)
{
factor *= increment;
}
else if (delta < 0 && factor > 0.25)
{
factor /= increment;
}
}
public Image ResizeImage(Size size)
{
return new Bitmap(img, size);
}