質問

私の追加画像をFlowLayoutPanel制御は、以下のコード

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

問題は、私が以下のようなエラ 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

ではなぜですか。

役に立ちましたか?

解決

Image.FromFile実際に負荷がにファイルをロックし、唯一のロックを解除それが配置されている一度ます。

の溶液を別の画像のグラフィックスコンテキスト(効果的にそれをコピーする)に画像を描画することで、元の画像を配置します。

他のヒント

Aha!ま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() 

更新: こうKonradされています。すべての開発には限り、僕:)

 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を終了しました。

私は、このソリューションは、それがピクチャボックスにロードされた後の画像ファイルのロックを解除することをお勧めしました

のPictureBoxName の.LOAD(の完全なパスとイメージファイル名の)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top