2010-10-24 47 views
2

我通過下面的代碼該進程無法訪問該文件

Dim WithEvents Pedit As DevExpress.XtraEditors.PictureEdit 

Private Sub LoadImagesCommon(ByVal fi As FileInfo) 
     Pedit = New DevExpress.XtraEditors.PictureEdit 
     Pedit.Width = 133 
     Pedit.Height = 98 
     Pedit.Image = Image.FromFile(fi.FullName) 
     Pedit.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom 
     Pedit.ToolTip = fi.Name 
     AddHandler Pedit.MouseClick, AddressOf Pedit_MouseClick 
     AddHandler Pedit.MouseEnter, AddressOf Pedit_MouseEnter 
     AddHandler Pedit.MouseLeave, AddressOf Pedit_MouseLeave 
     FlowLayoutPanel1.Controls.Add(Pedit) 
    End Sub 

問題將圖像添加到FlowLayoutPanel的控制是,我得到以下錯誤The process cannot access the file xxxx because it is being used by another process.當我嘗試刪除我在前一步加載的圖像。

    FlowLayoutPanel1.Controls.Clear() 
        FlowLayoutPanel1.Refresh() 
        For Each fi As FileInfo In New DirectoryInfo(My.Settings.TempDirectory).GetFiles 
         RemoveHandler Pedit.MouseClick, AddressOf Pedit_MouseClick 
         RemoveHandler Pedit.MouseEnter, AddressOf Pedit_MouseEnter 
         RemoveHandler Pedit.MouseLeave, AddressOf Pedit_MouseLeave 
         File.Delete(fi.FullName) 
        Next 

那麼我在這裏做錯了什麼?

回答

4

啊哈!謝謝Konrad。

經過一番閱讀,我發現了另一種解決方法。

Dim fs As System.IO.FileStream 
fs = New System.IO.FileStream(fi.FullName, IO.FileMode.Open, IO.FileAccess.Read) 
Pedit.Image = System.Drawing.Image.FromStream(fs) 
fs.Close() 

更新: 這裏是什麼康拉德建議。對於所有的新手在那裏,就像我:)

Dim imgTemp As System.Drawing.Image 
imgTemp = System.Drawing.Image.FromFile(strFilename, True) 
Pedit.Image = New System.Drawing.Bitmap(imgTemp) 
imgTemp.Dispose() 
Pedit.Image.Save(strFilename) 

哪個更好,因爲圖片對象不能擁有的FileStream已關閉後調用它的保存方法。

+0

您的解決方案(映像文件的完整路徑名)比我的好。 – 2010-10-24 20:11:05

+0

我相信正好相反。請看看更新。 – OrElse 2010-10-25 06:52:33

0

我發現這個解決方案是最好的解鎖圖像文件時,它已經被加載到圖片框後:

PictureBoxName .LOAD

相關問題