C# 如何在c中删除打开的picturebox的图像#

C# 如何在c中删除打开的picturebox的图像#,c#,C#,我有一个画框。 它从网络摄像头获取图像。我想从目录中删除它的图片并保存它的新图片。但在catch: The process cannot access the file '...\bin\Debug\dataBase\img\6.jpg' because it is being used by another process. 化学需氧量: 您可以使用下面的函数加载图像而不锁定它。加载位图后,您可以毫无问题地删除它。 用法: 功能: 您还可以使用以下功能删除只读文件: C#代码:

我有一个画框。 它从网络摄像头获取图像。我想从目录中删除它的图片并保存它的新图片。但在catch:

 The process cannot access the file '...\bin\Debug\dataBase\img\6.jpg' because it is being used by another process.
化学需氧量:


您可以使用下面的函数加载图像而不锁定它。加载位图后,您可以毫无问题地删除它。
用法:


功能:

您还可以使用以下功能删除只读文件:

C#代码:


似乎该文件已被使用,您可以在删除之前进行检查:i check。它表示进程无法访问文件“…\bin\Debug\dataBase\img\6.jpg”,因为它正被另一个进程使用。在catch块中。如何解决它?VB回答C#问题。
                  Bitmap bmp1 = new Bitmap(_pic_image.Image);


                    string path = Application.StartupPath + @"\dataBase\img\" + _txt_sufix.Text + ".jpg";
                    CheckIfFileIsBeingUsed(path);
                    if (System.IO.File.Exists(path))
                        System.IO.File.Delete(path);

                    bmp1.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
                    //_pic_image.Visible = true;
                    _pic_image.Image = bmp1;
                    _pic_image.Visible = true;
                    // Dispose of the image files.
                    bmp1.Dispose();


 public bool CheckIfFileIsBeingUsed(string fileName)
    {

        try
        {
            File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
        }

        catch (Exception exp)
        {

            return true;
        }

        return false;

    }
_pic_image.Image = OpenImageWithoutLockingIt("h:\myimage.png")
    Private Function OpenImageWithoutLockingIt(imagePath As String) As Bitmap
    If IO.File.Exists(imagePath) = False Then Return Nothing
    Using imfTemp As Image = Image.FromFile(imagePath)
        Dim MemImage As Bitmap = New Bitmap(imfTemp.Width, imfTemp.Height)
        Using g As Graphics = Graphics.FromImage(MemImage)
            g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
            g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
            g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
            g.Clear(Color.Transparent)
            g.DrawImage(imfTemp, 0, 0, MemImage.Width, MemImage.Height)
            Return MemImage
        End Using
    End Using
End Function
    Private Function DeleteImageFile(filePath As String, DeleteAlsoReadonlyFile As Boolean) As Boolean
    Try

        If IO.File.Exists(filePath) = False Then Return True
        If DeleteAlsoReadonlyFile Then
            Dim fileInf As New FileInfo(filePath)
            If fileInf.IsReadOnly Then
                'remove readonly attribute, otherwise File.Delete throws access violation exception.
                fileInf.IsReadOnly = False
            End If
        End If
        IO.File.Delete(filePath)
        Return True
    Catch ex As Exception
        Return False
    End Try
End Function
   private Bitmap OpenImageWithoutLockingIt(string imagePath)
    {
        if (System.IO.File.Exists(imagePath) == false)
            return null;
        using (Image imfTemp = Image.FromFile(imagePath))
        {
            Bitmap MemImage = new Bitmap(imfTemp.Width, imfTemp.Height);
            using (Graphics g = Graphics.FromImage(MemImage))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.Clear(Color.Transparent);
                g.DrawImage(imfTemp, 0, 0, MemImage.Width, MemImage.Height);
                return MemImage;
            }
        }
    }

    private bool DeleteImageFile(string filePath, bool DeleteAlsoReadonlyFile)
    {
        try
        {
            if (System.IO.File.Exists(filePath) == false)
                return true;
            if (DeleteAlsoReadonlyFile)
            {
                FileInfo fileInf = new FileInfo(filePath);
                if (fileInf.IsReadOnly)
                {
                    //remove readonly attribute, otherwise File.Delete throws access violation exception.
                    fileInf.IsReadOnly = false;
                }
            }
            System.IO.File.Delete(filePath);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }