简体   繁体   English

如何将图像表单目录加载到pictureBox 加载到picturebox 后删除图像文件并再次删除下一个图像?

[英]How can i load image form directory to pictureBox delete the image file after loaded to picturebox and again to next images?

int countimages = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {           
            Image img = sc.CaptureWindowToMemory(windowHandle);
            Bitmap bmp = new Bitmap(img,img.Width,img.Height);
            bmp.Save(@"e:\screenshotsofpicturebox1\screenshot" + countimages + ".bmp");
            bmp.Dispose();

            string[] images = Directory.GetFiles(@"e:\screenshotsofpicturebox1\", "*.bmp");
            if (images.Length > 0)
            {
                if (pictureBox1.Image != null)
                {
                    File.Delete(images[0]);
                    countimages = 0;
                    pictureBox1.Image.Dispose();
                }
                pictureBox1.Image = Image.FromFile(images[countimages]);
            }
            countimages += 1;
            label1.Text = countimages.ToString();
        }
  1. I want to save image to the hard disk我想将图像保存到硬盘
  2. load the image to the pictureBox1加载图片到pictureBox1
  3. after the image loaded to pictureBox1 delete the file from the hard disk图片加载到pictureBox1后,从硬盘中删除文件
  4. save a new image to the hard disk and load it to the pictureBox1将新图像保存到硬盘并将其加载到pictureBox1
  5. delete the file and so on each time with a new image.每次使用新图像删除文件等。

The problem for now is i'm getting exception on the line:现在的问题是我在线上遇到异常:

File.Delete(images[0]);

The process cannot access the file e:\\screenshotsofpicturebox1\\screenshot0.bmp because it is being used by another process.该进程无法访问文件e:\\screenshotsofpicturebox1\\screenshot0.bmp因为它正被另一个进程使用。

Another problem I saw now it's saving each time a new file to the hard disk我现在看到的另一个问题是每次将新文件保存到硬盘时

screenshot0.bmp
screenshot1.bmp

But I wan to be only one file each time screenshot0.bmp and just to replace it each time with a new image.但是我每次只想要一个文件screenshot0.bmp并且每次都用新图像替换它。

Reading your code I assume you are trying to show the screen in the picturebox on each tick event.阅读您的代码我假设您正在尝试在每个滴答事件的图片框中显示屏幕。 So, if that is your objective, you don't need to save/delete it, just assign the Image object to the PictureBox Image property like this:因此,如果这是您的目标,则不需要保存/删除它,只需将 Image 对象分配给 PictureBox Image 属性,如下所示:

private void timer1_Tick(object sender, EventArgs e) {
    Image refToDispose = pictureBox1.Image;
    pictureBox1.Image = sc.CaptureWindowToMemory(windowHandle);
    refToDispose.Dispose();
}

If you want to save/delete it anyway, you can't pass a directly loaded bitmap from the file to the PictureBox because it will lock the file while in use.如果你想保存/删除它,你不能将直接加载的位图从文件传递到图片框,因为它会在使用时锁定文件。

Instead you can create a graphics object from another Bitmap instance with the size of the image and draw it, so the new Bitmap will be a copy without the file lock that you can assign to the PictureBox.相反,您可以从另一个具有图像大小的 Bitmap 实例创建一个图形对象并绘制它,因此新 Bitmap 将是一个副本,没有您可以分配给 PictureBox 的文件锁。

In your code change this:在你的代码中改变这个:

pictureBox1.Image = Image.FromFile(images[countimages]);

To this:对此:

using (Image imgFromFile = Image.FromFile(images[countimages])) {
    Bitmap bmp = new Bitmap(imgFromFile.Width, imgFromFile.Height);
    using (Graphics g = Graphics.FromImage(bmp)) {
        g.DrawImage(imgFromFile, new Point(0, 0));
    }
    pictureBox1.Image = bmp;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM