简体   繁体   English

即使使用.Dispose()也无法释放文件

[英]Files not being released, even with .Dispose()

I have a simple C# application for renaming and resizing images, and I have a little problem - when it completes working with all given files, the last one always stays in the memory, or whatever, of the program, and I can't delete it without closing the program. 我有一个用于重命名和调整图像大小的简单C#应用程序,还有一个小问题-当它完成了对所有给定文件的处理后,最后一个总是保留在程序的内存中,或者其他任何内容,我无法删除它没有关闭程序。

How can I make my code always release the files it works with after it has finished? 如何使我的代码在完成后始终释放其可使用的文件?

here's what I have 这就是我所拥有的

     private void processFiles() {
        foreach (string oldFileNamePath in fileEntries) {
            ...
            File.Move(oldFileNamePath, newFileNamePath);
            if (isResizeImage) {
                if (!extension.Equals(".gif")) {
                    Image newImage = Image.FromFile(newFileNamePath);
                    int newWidth = (int) (newImage.Width * (1 + ((double) percentageSize / 100)));
                    int newHeight = (int) (newImage.Height * (1 + ((double) percentageSize / 100)));
                    newImage = ResizeImage(newFileNamePath, newWidth, newHeight);
                    newImage.Save(directory + RESIZED_DIRECTORY + "\\" + newFileName, ImageFormat.Jpeg);
                    newImage.Dispose();
                } else {
                    // only move
                    File.Move(newFileNamePath, directory + RESIZED_DIRECTORY + "\\" + newFileName);
                }
            }
        }
    }

    public static Bitmap ResizeImage(String path, int width, int height) {
        Image image = Image.FromFile(path);
        var destRect = new Rectangle(0, 0, width, height);
        var destImage = new Bitmap(width, height);
        destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
        using (var graphics = Graphics.FromImage(destImage)) {
            // process image
            using (var wrapMode = new ImageAttributes()) {
                wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
            }
        }
        image.Dispose();
        return destImage;
    }
}

The original newImage isn't exposed. 原始的newImage不公开。 You are reassigning it and disposing the new imageNew you are receiving from ResizeImage. 您将重新分配它并处理新图像。

Remove the following: 删除以下内容:

Image newImage = Image.FromFile(newFileNamePath);

And move your declaration to the next line. 并将您的声明移至下一行。 This line didn't do anything, since you replace it immediately after and dispose the new replacement, but never the original. 该行没有执行任何操作,因为您在之后立即替换并处置了新的替换项,但从未处理过。

After edit of OP: 编辑OP之后:

You should do this: 你应该做这个:

Image newImage = Image.FromFile(newFileNamePath); 
int newWidth = (int) (newImage.Width * (1 + ((double) percentageSize / 100)));
int newHeight = (int) (newImage.Height * (1 + ((double) percentageSize / 100)));
newImage.Dispose();

Image anotherImage = ResizeImage(newFileNamePath, newWidth, newHeight))
anotherImage.Save(directory + RESIZED_DIRECTORY + "\\" + newFileName, ImageFormat.Jpeg);
anotherImage.Dispose();

For explanation purposes I did leave out the use of using. 为了便于说明,我确实省略了using的使用。 But normally you should do that: 但是通常您应该这样做:

using (Image newImage = Image.FromFile(newFileNamePath))
{
    int newWidth = (int) (newImage.Width * (1 + ((double) percentageSize / 100)));
    int newHeight = (int) (newImage.Height * (1 + ((double) percentageSize / 100)));
    using (Image anotherImage = ResizeImage(newFileNamePath, newWidth, newHeight))
        anotherImage.Save(directory + RESIZED_DIRECTORY + "\\" + newFileName, ImageFormat.Jpeg);
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM