0

无法删除文件夹中的图像文件。我知道它被另一个进程锁定和使用,但我不知道哪个进程持有它以及如何解锁图像文件,以便我可以删除它。试图通过将.Dispose()方法放入每个循环结尾来猜测,但没有运气。>_<

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

using System.Net;
using System.Drawing;
using System.IO;

using AForge.Video;
using AForge.Video.VFW; //video recording

namespace example
{
    public partial class LiveRecording2 : System.Web.UI.Page
    {
        AVIWriter writer = new AVIWriter("MSVC");

        protected void Page_Load(object sender, EventArgs e)
        {

        }

         private void Get1Image()
        {
             for (int i = 0; i < 11; i++)
             {
                string ImagePath = Server.MapPath("~\\Videos\\liveRecording2\\") + string.Format("{0}", i + 1) + ".jpg";
                string ip = "http://example.ip.com:portNo/jpeg.cgi";
                string sourceURL = ip;
                WebRequest req = (WebRequest)WebRequest.Create(sourceURL);
                req.Credentials = new NetworkCredential("userName", "password");
                WebResponse resp = req.GetResponse();
                Stream stream = resp.GetResponseStream();
                Bitmap bmp = (Bitmap)Bitmap.FromStream(stream);
                bmp.Save(ImagePath);
            }
        }

        private void doRecording()
        {
            string ImagePath = Server.MapPath("~\\Videos\\liveRecording2\\");
            string SavingPath = Server.MapPath("~\\Videos\\liveRecording2\\"); //recorded video path
            string VideoName = "ICS-" + String.Format("{0:yyyyMMdd_hhmmss}", DateTime.Now) + ".avi";
            writer.Open(SavingPath + VideoName, 640, 480); //create an AVI file and open it for images adding

            // create frame image
            Bitmap image = new Bitmap(320, 240);
            //var cubit = new AForge.Imaging.Filters.ResizeBilinear(320, 240);
            string[] files = Directory.GetFiles(ImagePath);
            writer.FrameRate = 25;
            int index = 0;
            int failed = 0;
            foreach (var item in files)
            {
                index++;
                try
                {
                    image = Image.FromFile(item) as Bitmap;
                    //image = cubit.Apply(image);

                    for (int i = 0; i < 5; i++)
                    {
                        writer.AddFrame(image);
                    }
                }
                catch
                {
                    failed++;
                }
                //this.Text = index + " of " + files.Length + ". Failed: " + failed;
             }
            writer.Close();
            writer.Dispose();
            this.Label1.Text = "status: Video was successfully created";
            this.Label2.Text = "Video Path: ~/Videos/liveRecording2/" + VideoName;
            DeleteImage();
        }

       protected void Button1_Click(object sender, EventArgs e)
        {
            Get1Image();
            doRecording();
        }

        public void DeleteImage()
        {
            DirectoryInfo di = new DirectoryInfo(Server.MapPath("~\\Videos\\liveRecording2\\"));
            FileInfo[] files = di.GetFiles("*.jpg");
                             //.Where(p => p.Extension == ".jpg").ToArray();
            foreach (FileInfo file in files)
            {
                file.Delete(); // **error here**
            }

            this.Label3.Text = "Image deleted";
        }
    }
} 
4

1 回答 1

0

你的问题可能在这里:

image = Image.FromFile(item) as Bitmap;

尝试将那段代码包装在using

using(image = Image.FromFile(item) as Bitmap)
{
   //  rest here
}
于 2012-10-15T04:35:14.217 回答