简体   繁体   English

我应该如何以及在哪里放置旧图像? 以及将pictureBox中的图像设置为最小和最大的大小是多少?

[英]How and where should i dispose the old image ? And to what size to set the image in the pictureBox as minimum and maximum?

Im using the mouse wheel to zoom in/out an image in a pictureBox: 我使用鼠标滚轮放大/缩小图片框中的图像:

In the top of form1: 在form1的顶部:

double increment = 1.25;
double factor = 1.0;
Image img;

In the constructor: 在构造函数中:

img = new Bitmap(@"d:\radar000075.png");
pictureBox1.Load(@"d:\radar000075.png");

Then two events and resize mthod: 然后发生两个事件并调整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));
        }

Now its working without any problems but: 现在,它的工作没有任何问题,但是:

  1. In the mouse wheel event i assign to the pictureBox.Iamge each time the original image and using the resizeImage method. 在鼠标滚轮事件中,我每次将原始图像分配给pictureBox.Iamge并使用resizeImage方法。 Do i need to dispose somewhere each time the original image (img) ? 每次原始图像(img)都需要放置在某个地方吗? Or the old image i assigned somehow ? 还是我以某种方式分配的旧图片?

  2. How and where do i set the limits for the image resize now its resizing the image by 25% small or big but there should be a limit how big i can zoom in and how small i can zoom out where and how do i set this limits ? 我如何以及在哪里设置图像调整大小的限制,现在将图像调整大小的大小为25%,但是应该有一个限制,即我可以放大多少,我可以缩小多少,在哪里以及如何设置此限制?

Bitmap implements the IDisposable interface so, yes, technically you should dispose of it once you're finished using it. Bitmap实现了IDisposable接口,因此,是的,从技术上讲,您应该在完成使用后将其丢弃。 Otherwise it will be up to the Garbage Collector to do it, and you never know when it will trigger. 否则,将由垃圾收集器来执行,而您永远不知道何时触发。 So you may see your memory consumption go up and down if you resize a lot of times because of the new bitmap you're creating each time. 因此,由于每次都要创建新的位图,因此多次调整大小可能会看到内存消耗增加和减少。

For the factor limitation, personally I'd extract the current factor calculation code from pictureBox1_MouseWheel and add it in the new method. 对于因子限制,我个人将从pictureBox1_MouseWheel提取当前因子计算代码,并将其添加到新方法中。 It's not really the event business to know about resizing so it should be in a separate method (You should take a look at the Single Responsibility Principle ). 调整大小并不是真正的事件业务,因此应该采用单独的方法(您应该查看“ 单一职责原则” )。 Simply adding an other condition to your already existing if could easily do the job. 如果可以轻松完成工作,只需在现有的条件上添加其他条件即可。 It could be something along those lines : 可能是沿着这些思路:

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;
    }
}

Basically you resize only if the factor is smaller than, let's say, 2 and greater than 0.25. 基本上,仅当因子小于等于2且大于0.25时才调整大小。

Also, here's some quick recommendations for your code : 另外,这是您的代码的一些快速建议:

  • Methods in C# are usually typed in CamelCase, so resizeImage should be ResizeImage . C#中的方法通常在CamelCase中键入,因此resizeImage应该是ResizeImage
  • img is already declared at class level, you do not need to pass it to resizeImage as a parameter img已经在类级别声明,您无需将其作为参数传递给resizeImage
  • avoid using the static modifier if you don't intend to use the method statically. 如果您不打算静态使用该方法,请避免使用static修饰符。
  • You do not need to cast the bitmap as an Image 您无需将位图转换为图像

so your method can be simplified to 这样您的方法可以简化为

public Image ResizeImage(Size size)
{
    return new Bitmap(img, size);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pictureBox图像处理异常 - pictureBox image dispose exception 如果我替换PictureBox控件中的图像,我应该首先处理原始图像吗? .Net Winforms - If I replace an image in a PictureBox control, should I dispose the original image first? .Net Winforms 在不丢失图片框的情况下处理图片框图像 - Dispose of a pictureBox image without losing the pictureBox 处理 Image/Bitmap 和 PictureBox 的正确方法 - Right way to dispose Image/Bitmap and PictureBox 如何为MonoGame设置最小/最大窗口大小? - How to set Minimum/Maximum Window Size for MonoGame? winForms中点击size属性设置为CenterImage的pictureBox如何获取图片的实际坐标? - How can I get the actual coordinates of the image when I click on the pictureBox whose size property is set to CenterImage in winForms? 如何获得图片框内图像的当前大小 - How to get the current size of an image within a picturebox WinForms-UserControl.Dispose也可以在PictureBox中放置图像吗? - WinForms - Will UserControl.Dispose dispose Image in PictureBox as well? 如何通过继承PictureBox的类中的代码设置PictureBox的图像? - How do I set the image of a PictureBox through code in a class that inherits PictureBox? PictureBox 大小和图像大小(以像素为单位)之间的关系是什么? - What is the relation between the PictureBox size and the image size in pixels?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM