我的程序保留使用 GDI 编写的文件

发布于 2024-08-24 09:03:01 字数 496 浏览 4 评论 0原文

  private void save(Rectangle src, Rectangle dest, string fName, int width, int height, Bitmap file)
        {
            Bitmap result = new Bitmap(width, height);

            Graphics g = Graphics.FromImage(result);

            g.DrawImage(file, dest, src, GraphicsUnit.Pixel);
            result.Save(fName);

            g.Dispose();
            result.Dispose();
            result = null;
            g = null;

        }

它确实会写入文件,但由于某种原因,即使程序关闭,它也会保留它们。

  private void save(Rectangle src, Rectangle dest, string fName, int width, int height, Bitmap file)
        {
            Bitmap result = new Bitmap(width, height);

            Graphics g = Graphics.FromImage(result);

            g.DrawImage(file, dest, src, GraphicsUnit.Pixel);
            result.Save(fName);

            g.Dispose();
            result.Dispose();
            result = null;
            g = null;

        }

It does write the files but for some reason even if the program is closed, it keeps hold of them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

君勿笑 2024-08-31 09:03:01

尝试使用重载与流作为位图的参数.Save() 方法。然后您可以处理作为参数传递的 FileStream。

Try to use the overload with a Stream as a parameter of the Bitmap.Save() method. Then you can dispose the FileStream you pass as the argument.

内心激荡 2024-08-31 09:03:01

它确实会写入文件,但对于某些
即使程序关闭,原因
它抓住了它们。

不可能。当进程退出时操作系统会关闭所有文件。必须是打开该文件的其他进程。

使用 Process Explorer (http://technet.microsoft.com/en-us /sysinternals/bb896653.aspx)Find,Find Handle菜单命令对文件名进行确定的过程。

It does write the files but for some
reason even if the program is closed,
it keeps hold of them.

Not possible. The operating system closes all files when the process exits. Must be some other process that has the file open.

Use the Process Explorer (http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx) Find, Find Handle menu command on the file name to determine the process.

我还不会笑 2024-08-31 09:03:01

首先,停止将变量引用设置为 null - 这在 .NET 中没有显着影响。垃圾收集器决定变量超出范围后何时释放内存。

其次,您在这里没有正确处理异常。 C# 为您提供 tryusing 结构是有原因的:使用它们。

using (Bitmap result = new Bitmap(width, height))
{
    using (Graphics g = Graphics.FromImage(result))
    {
        g.DrawImage(file, dest, src, GraphicsUnit.Pixel);
    }
    result.Save(fName);
}

这段代码更容易阅读,并且如果发生意外情况也不会泄漏资源。

如果您发现文件本身仍然被锁定或者 GDI 资源仍然泄漏,那么可能是其他原因造成的 - 上面的代码不能泄漏资源,也不能保持文件锁定。 (除非发生某些致命异常,但在这种情况下你的整个程序无论如何都会崩溃)

First of all, stop setting variable references to null - that has no significant effect in .NET. The garbage collector decides when to free memory after variables go out of scope.

Second, you're not handling exceptions properly here. C# gives you the try and using constructs for a reason: use them.

using (Bitmap result = new Bitmap(width, height))
{
    using (Graphics g = Graphics.FromImage(result))
    {
        g.DrawImage(file, dest, src, GraphicsUnit.Pixel);
    }
    result.Save(fName);
}

This code is a lot easier to read and won't leak resources if something unexpected happens.

If you're finding that the file itself is still locked or that GDI resources are still leaking, then something else is responsible - the above code cannot leak resources nor keep a file locked. (Except in the event of certain fatal exceptions, but in that case your whole program is going down anyway)

我的鱼塘能养鲲 2024-08-31 09:03:01

谢谢大家。根据进程资源管理器,这是我正在使用的一些 PS3 控制器软件以及诺基亚 PC 套件。

Thanks to everyone. According to process explorer it was some ps3 controller software I was using and also nokia pc suite.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文