尝试删除文件时为什么要让文件忙?

时间:2016-04-26 05:52:30

标签: c# .net winforms

在backgroundworker dowork事件中

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    try
    {
        for (int i = 0; i < 1000; i++)
        {
            try
            {
                System.Threading.Thread.Sleep(1);
                pictureBox1.Invoke(new Action(() =>
                {
                    if (pictureBox1.Image != null)
                        pictureBox1.Image.Dispose();
                    pictureBox1.Image = sc.CaptureWindowToMemory(windowHandle);
                }));

                pictureBox1.Invoke(new Action(() =>
             {
                 string[] images = Directory.GetFiles(@"e:\screenshotsofpicturebox1\", "*.bmp");
                 if (pictureBox2.Image != null)
                 {
                     pictureBox2.Image.Dispose();
                     File.Delete(images[0]);
                 }
                 countimages = 0;
                 Bitmap bmp = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);
                 bmp.Save(@"e:\screenshotsofpicturebox1\screenshot" + countimages + ".bmp");
                 bmp.Dispose();
                 if (images.Length > 0)
                 {
                     images = Directory.GetFiles(@"e:\screenshotsofpicturebox1\", "*.bmp");
                     pictureBox2.Image = Image.FromFile(images[0]);
                     Bitmap bmp1 = new Bitmap(Image.FromFile(images[0]));
                     CreateMovie(bmp1);
                 }
                 countimages += 1;
             }));
            }
            catch (Exception err)
            {
                string myerr = err.ToString();
            }
        }
    }
    catch (Exception err)
    {
        string error = err.ToString();
    }
}

方法CreateMovie

private void CreateMovie(Bitmap image)
{
    int width = 640;
    int height = 480;

    VideoFileWriter writer = new VideoFileWriter();
    writer.Open(@"e:\screenshotsofpicturebox1\videotest.avi", width, height, 25, VideoCodec.MPEG4, 1000000);

    image = new Bitmap(width, height);
    Graphics g = Graphics.FromImage(image);
    g.FillRectangle(Brushes.Black, 0, 0, width, height);
    Brush[] brushList = new Brush[] { Brushes.Green, Brushes.Red, Brushes.Yellow, Brushes.Pink, Brushes.LimeGreen };
    Random rnd = new Random();

    for (int i = 0; i < 250; i++)
    {
        int rndTmp = rnd.Next(1, 3);
        Application.DoEvents();
        g.FillRectangle(brushList[i % 5], (i % width) * 2, (i % height) * 0.5f, i % 30, i % 30);
        g.FillRectangle(brushList[i % 5], (i % width) * 2, (i % height) * 2, i % 30, i % 30);
        g.FillRectangle(brushList[i % 5], (i % width) * 0.5f, (i % height) * 2, i % 30, i % 30);
        g.Save();
        writer.WriteVideoFrame(image);
    }

    g.DrawString("(c) 2013 by code-bude.net", new System.Drawing.Font("Calibri", 30), Brushes.White, 80, 240);
    g.Save();
    for (int i = 0; i < 125; i++)
    {
        writer.WriteVideoFrame(image);
    }
    writer.Close();
    image.Dispose();
}

backgroundworker dowork中的代码工作正常,直到我添加了CreateMovie方法。

我正在做的是将位图保存到硬盘上并将其显示在pictureBox1中,然后删除文件并将下一个图像保存到硬盘显示中,将其删除等等。

现在我从每个保存的位图文件中添加了我想要实时创建的CreateMovie方法视频文件。

现在,当调用CreateMovie时,我在行上获得了异常:

File.Delete(images[0]);

但是在CreateMovie方法的底部,我发布的是Bitmap,我正在做

image.Dispose();

但仍然有例外。

0 个答案:

没有答案