如何从位图释放文件以将其删除. [英] how to release file from bitmap to delete it.

查看:71
本文介绍了如何从位图释放文件以将其删除.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


位图占用文件"targetImage.jpg".请查看下面的代码,该代码提出了我面临的真正问题.我创建了一个单独的程序来澄清问题.

Hi,
bitmap occupies the file "targetImage.jpg". Please see the code below that presents the real problem I am facing. I have created a separate program to clarify the problem.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace ALL_RFID_PROJECTS_1._0._2
{
    static class PROGRAM
    {
        static void Main()
        {
            Bitmap b = new Bitmap(@"D:\01.jpg");
            System.IO.File.Delete(@"D:\01.jpg");
        }


    }
}


异常消息为=该进程无法访问文件"D:\ 01.jpg",因为它正在被另一个进程使用."
如何从位图释放文件,以便将其删除.
请帮助我.


The Exception Message is="The process cannot access the file ''D:\01.jpg'' because it is being used by another process."
How can I release the file from bitmap so I can delete it.
Please help me.

推荐答案

如果在位图上调用dispose,则可以将其删除而没有任何问题.

If you call dispose on the bitmap, it can be deleted without any problems.

Bitmap b = new Bitmap(@"D:\01.jpg");
b.Dispose(); // now the bitmap is released
System.IO.File.Delete(@"D:\01.jpg");



干杯

Manfred



Cheers

Manfred


删除第一行:
Bitmap b = new Bitmap(@"D:\01.jpg");


并重试.


and try again.


Bitmap temp = new Bitmap(@"D:\01.jpg");
Bitmap b = (Bitmap)temp.Clone();
temp.Dispose(); // now temp is released
System.IO.File.Delete(@"D:\01.jpg");
// you may safely use b from now on and forget about temp...


并且,请不要将任何文件放在驱动器的根目录中……在Windows Vista/Seven中,您可能会遇到麻烦!改用子目录.


这篇关于如何从位图释放文件以将其删除.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆