我的程序保留使用 GDI 编写的文件
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用重载与流作为位图的参数.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.
不可能。当进程退出时操作系统会关闭所有文件。必须是打开该文件的其他进程。
使用 Process Explorer (http://technet.microsoft.com/en-us /sysinternals/bb896653.aspx)Find,Find Handle菜单命令对文件名进行确定的过程。
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.
首先,停止将变量引用设置为
null
- 这在 .NET 中没有显着影响。垃圾收集器决定变量超出范围后何时释放内存。其次,您在这里没有正确处理异常。 C# 为您提供
try
和using
结构是有原因的:使用它们。这段代码更容易阅读,并且如果发生意外情况也不会泄漏资源。
如果您发现文件本身仍然被锁定或者 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
andusing
constructs for a reason: use them.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)
谢谢大家。根据进程资源管理器,这是我正在使用的一些 PS3 控制器软件以及诺基亚 PC 套件。
Thanks to everyone. According to process explorer it was some ps3 controller software I was using and also nokia pc suite.