为什么 GDI 不允许我删除大图像?
我的 ASP.NET 应用程序具有图像裁剪和调整大小功能。这需要删除上传的临时图像。一切正常,但当我尝试删除大于 80px x 80px 的图像时,我收到“文件被另一个进程锁定...”错误,即使我已经释放了所有资源。
这是一个片段:
System.Drawing.Image tempimg = System.Drawing.Image.FromFile(temppath);
System.Drawing.Image img = (System.Drawing.Image) tempimg.Clone(); //advice from another forum
tempimg.Dispose();
img = resizeImage(img, 200, 200); //delete only works if it's 80, 80
img.Save(newpath);
img.Dispose();
File.Delete(temppath);
My ASP.NET application has an image cropping and resizing features. This requires that the uploaded temporary image be deleted. Everything works fine, but when I try to delete an image larger than 80px by 80px I get a "File is locked by another process..." error, even though I've released all resources.
Here's a snippet:
System.Drawing.Image tempimg = System.Drawing.Image.FromFile(temppath);
System.Drawing.Image img = (System.Drawing.Image) tempimg.Clone(); //advice from another forum
tempimg.Dispose();
img = resizeImage(img, 200, 200); //delete only works if it's 80, 80
img.Save(newpath);
img.Dispose();
File.Delete(temppath);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您没有处理分配给 img 变量的第一个 Image 实例。
考虑一下这个:
I think you are not disposing the first Image instance assigned to the img variable.
Consider this instead:
如果您以这种方式创建图像,它将不会被锁定:
If you create the image this way, it won't be locked: