Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using the following code to load the ListView and ImageList:
Code:
VB
'Populate screenshots
        ScrnShtDirLbl.Text = ScrnShts
        ToolTip1.SetToolTip(ScrnShtDirLbl, ScrnShts)
        ScrnShtsLst.Items.Clear()
        ImageList1.Images.Clear()
        For Each item In IO.Directory.GetFiles(ScrnShts)
            ImageList1.Images.Add(item, LoadImage(item).Clone)
            'Img.Dispose()
            Dim fi As New IO.FileInfo(item)
            Dim It As New ListViewItem With {
                .Text = fi.Name,
                .Tag = item,
                .ImageKey = item
            }

            ScrnShtsLst.Items.Add(It)
        Next

Code:
VB
Function LoadImage(filepath As String) As Image
        Using imgTemp As New Bitmap(filepath) ' Temporarily load image into a variable rather than into the ImageList directly.
            Return imgTemp.Clone
            imgTemp.Dispose()
        End Using
    End Function

Then to delete selected items:
Code:
VB
Private Sub DeleteScrnShtBtn_Click(sender As Object, e As EventArgs) Handles DeleteScrnShtBtn.Click
        If ScrnShtsLst.CheckedItems.Count > 0 Then
            For Each itm As ListViewItem In ScrnShtsLst.CheckedItems
                Try

                    'ImageList1.Images.Keys.Remove(itm.Tag.ToString)
                    ImageList1.Images.RemoveByKey(itm.Tag.ToString)
                    'ImageList1.Images.Item(itm.Index).Dispose()
                    'ImageList1.Images.RemoveAt(itm.Index)

                    ScrnShtsLst.Items.Remove(itm)
                    'ScrnShtsLst.Items.Item(itm.Index).Remove()

                    Application.DoEvents()
                    'IO.File.Delete(itm.Tag.ToString)
                    'FilesToDelete.Add(itm.Tag.ToString)
                    My.Computer.FileSystem.DeleteFile(itm.Tag.ToString, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin)
                    
                Catch ex As Exception
                    My.Computer.Clipboard.SetText(ex.Message)
                    MsgBox(ex.Message)
                End Try
            Next
            ReLoadLists()
        End If

The Try/Catch Ex.Message is: "The process cannot access the file 'C:\Users\'User'\AppData\Roaming\.minecraft\screenshots\2022-12-03_06.44.22.png' because it is being used by another process."

What I have tried:

The code above is from several posts found in search results. Nothing seems to work. Please help.
Posted
Updated 28-Dec-22 19:13pm
Comments
Graeme_Grant 28-Dec-22 22:52pm    
It is suggesting that an app has the file open. Are there any other active apps using that image file? It could also be that your app crashed and the file was left open. Reboot the computer, load VS and the app and run, it may fix it.
Member 15627495 29-Dec-22 1:20am    
you must stamp all the pictures you use in a 'temp' folder , that is to prevent 'fileLock' by read, and make all contents 'ready to work'
and after using them, you delete the temp folder content.
Amerigoware 30-Dec-22 9:03am    
Thanks for the suggestion. I will try that. I suspect I will have to clear the temp folder on the next start up though, but that's okay.

1 solution

Image.Clone creates a copy of the Image object, but the actual image data is shared between the two images until you write to it!

That may sound weird, but I suspect it's a performance thing - the long-winded operation of copying the actual bitmap data is deferred until it's really needed.

Have a look here: https://stackoverflow.com/questions/12709360/whats-the-difference-between-bitmap-clone-and-new-bitmapbitmap[^] where he talks about some experiments he did with new and clone.

Instead of Clone, create a new image and return that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900