2014-06-21 38 views
-1

我有一個圖片框。 它得到的圖像從webcam.i想從目錄中刪除它的圖片,並保存它的新picture.but上線這個錯誤說‘File.Delete(Application.S ......’在追趕:如何刪除c中打開的picturebox的圖像#

The process cannot access the file '...\bin\Debug\dataBase\img\6.jpg' because it is being used by another process. 

鱈魚:

    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; 

    } 
+0

看起來像文件被使用,你可以檢查之前刪除:http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in -use – Milen

+0

我檢查。它說它進程無法訪問文件'... \ bin \ Debug \ dataBase \ img \ 6.jpg',因爲它正在被另一個進程使用。在catch塊中,如何解決它? – user3374618

回答

-1

您可以使用以下函數來加載圖像而不鎖定它加載位圖後,您可以刪除它沒有任何問題
用法:

_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 

C#代碼:

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; 
     } 
    } 
+0

VB回答一個C#問題。 – Corey