none
Can't delete an icon after showing it. ("Dispose" gives error.) RRS feed

  • Question

  • I've written a utility that reads hundreds of icons from a folder, puts them in an Image array, and displays each in a PictureBox. One of the features I wish to add is the ability to delete selected icons on command. But when I try, I get a "File is in Use" error

    I set the specified image array entry to "Nothing" but that didn't help. So I added a ".dispose" method immediately after that just gives me an error:

    Dispose Error

    (The Form_Load contains):

    myBitmap = Image.FromFile(strIconPath & "Save\" & strCategory & Format(intCnt + 1, "00") & ".png")
    myThumbnail = myBitmap.GetThumbnailImage(256, 256, myCallback, IntPtr.Zero)
    thumbnails(intCnt).Image = myThumbnail
    [...]
    thumbnails = New PictureBox() {picThumb01, picThumb02, picThumb03, picThumb04, picThumb05, [...] picThumb195, picThumb196, picThumb197, picThumb198, picThumb199, picThumb200}


    If I put the Dispose() before the "Nothing" assignment, I get a "File Open" error. If I remove the "Nothing" assignment entirely, I get the same "File In Use" error as before. I've even tried replacing the displayed icon to another image, but I still get a "File In Use" error.

    I have no idea what I must do to release the image so I can delete it. All online searches say to use "Dispose", but I already am. :(

    TIA


    Tuesday, July 9, 2019 1:07 PM

Answers

  • Hi,
    at first you must release the reference to the image from PictureBox then remove the reference image from array "thumbnails" and then you can execute "Dispose()". If the user can click twice the btnDelete then you must check the reference in array ( = Nothing!).

    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Tuesday, July 9, 2019 1:22 PM

All replies

  • Hi,
    at first you must release the reference to the image from PictureBox then remove the reference image from array "thumbnails" and then you can execute "Dispose()". If the user can click twice the btnDelete then you must check the reference in array ( = Nothing!).

    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Tuesday, July 9, 2019 1:22 PM
  • Hi

    Here is some code that cycles through folder of Images and allows Delete (from HDD) of currently viewed Image.

    ' Form1 with PictureBox1,
    ' Button1 (+), Button2 (-)
    ' and Button3 (Del)
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim ImPath As String = "C:\Users\lesha\Desktop\Plans\Dice 2"
        Dim images As New List(Of String)
        Dim pos As Integer = 0
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            images = IO.Directory.GetFiles(ImPath, "*.png").ToList
            With PictureBox1
                .SizeMode = PictureBoxSizeMode.Zoom
            End With
            pos = 0
            ShowPic()
        End Sub
        Sub ShowPic()
            PictureBox1.Image = Image.FromFile(images(pos))
            Label1.Text = IO.Path.GetFileNameWithoutExtension(images(pos))
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            pos += 1
            If pos > images.Count - 1 Then pos = 0
            ShowPic()
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            pos -= 1
            If pos < 0 Then pos = images.Count - 1
            ShowPic()
        End Sub
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            Dim curr As String = images(pos)
    
            PictureBox1.Image.Dispose()
    
            images.Remove(curr)
            IO.File.Delete(curr)
            ShowPic()
        End Sub
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Tuesday, July 9, 2019 2:58 PM
    Tuesday, July 9, 2019 2:11 PM
  • Hi,
    at first you must release the reference to the image from PictureBox then remove the reference image from array "thumbnails" and then you can execute "Dispose()". If the user can click twice the btnDelete then you must check the reference in array ( = Nothing!).

    Thanks for the reply.

    How do I "release the reference"? I thought that's what assigning it to "Nothing" & "Dispose" were both supposed to do?

    TIA

    Tuesday, July 9, 2019 2:34 PM
  • Hi

    Here is some code that cycles through folder of Images and allows Delete (from HDD) of currently viewed Image.

            images.Remove(curr)
            IO.File.Delete(curr)
    


    Thanks, but I think I'd have to rewrite too much code to assign images to a "New List(Of String)" in order to use the ".Remove" method.

    I might try it if I get desperate, but hopefully there's an easier fix. ;)

    Thx.

    Tuesday, July 9, 2019 2:53 PM
  • Okay, I think I found a solution.

    The first error (in screenshot) was due to assigning the image to "Nothing" before trying to "Dispose" of it.

    The "solution" (ugly but works) was to "Dispose" first, then assign a blank image to the thumbnail so it is no longer on-screen. Trying to assign "Nothing" to the image was causing another error altogether. It needed to be another image.

    This releases all hooks into the .image so that it may be deleted successfully.

    Thanks.

    Tuesday, July 9, 2019 7:48 PM