Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete an image not working properly

Status
Not open for further replies.

mThomas

Instructor
May 3, 2001
404
US
I have a click event which is supposed to delete an image. I have a dropdown which displays all the images in a folder. When I try and use the dropdown selected value, the image is not deleted. However, if I pass the name of an image, the image is deleted. In the code below, when I hard code the image name ( commented out below ) the code works, but when I use the dropdown value it doesn't.

Any ideas?

mike

Code:
    Protected Sub godeleteimage_Click(ByVal sender As Object, ByVal e As EventArgs) Handles godeleteimage.Click

        If deleteimage.Checked = True Then

            'Dim ImageName = "test1thumb.png"
            Dim ImageName = imagemanagerdropdown.SelectedValue.ToString

            Dim ImagePath As [String] = [String].Format("{0}\{1}", Server.MapPath("gallery"), ImageName)

            If System.IO.File.Exists(ImagePath) Then
                System.IO.File.Delete(ImagePath)
            End If

        End If

        UpdatePanelImageManager.Update()


    End Sub
 

Have you verified that the path generated in this line:

Dim ImagePath As [String] = [String].Format("{0}\{1}", Server.MapPath("gallery"), ImageName)

is indeed valid?

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thank you for taking the time to reply.

Yes, the path is valid. The way I verified the path was to set the value of ImageName to a hard coded value:

Dim ImageName = "test1thumb.png"

When I set and use the above value for ImageName, the path works and the image is deleted.

I've even tried creating a bitmap variable set to an image, but that didn't work either.

mike

 

No, I mean when the image name is retrieved from the dropdown, have you stepped through the code and made sure that ImagePath is correct (e.g., no spaces, extra backslashes, etc.)?


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Yes, the path is ok. There are no errant spaces or extraneous characters. I am going to work on this some more, and I'll post back an update.
 
Ok, when I select an image from the dropdown list and attempt to delete it I get the following error message when the delete is in a try catch block:

"The process cannot access the file 'D:\sites\gallery\test1thumb.png' because it is being used by another process."

If I hard code the image name and do not select the image from the drop down, the image is deleted. However, even if the image is hard coded (as in the code below), and I select the hard coded image from the drop down, I get the above error. With the below code, as long as I do not select the same image in the drop down, the image is deleted, but if I select it I get the error. If I comment out the hard coded ImageName and uncomment out the ImageName set to the selected value in the drop down, I get the error.

So is it possible to unlock/close/dispose the image? Do I have to read the image into a file stream?

Here is my amended code with the try catch block:

Code:
    Protected Sub godeleteimage_Click(ByVal sender As Object, ByVal e As EventArgs) Handles godeleteimage.Click

        If deleteimage.Checked = True Then

            Try


                Dim ImageName = "test1thumb.png"
                'Dim ImageName = imagemanagerdropdown.SelectedValue.ToString

                Dim ImagePath As [String] = [String].Format("{0}\{1}", Server.MapPath("gallery"), ImageName)

                If System.IO.File.Exists(ImagePath) Then
                    System.IO.File.Delete(ImagePath)
                End If

            Catch ex As Exception
                errorlabel.Text = ErrorToString()
            End Try

        End If

        UpdatePanelImageManager.Update()


    End Sub
 
I solved the issue. In another portion of my web app, I was creating a variable as a new image bitmap, and when I was done with the image (getting height and width) I should have disposed the variable. In this case, the variable was using the same drop down. So because I did not dispose of the bitmap object, when I attempted to delete the image, I was getting the error.

So, the solution was to use Dispose() to unlock the image.

The new bit map was being created in a SelectedIndexChanged event.

Code:
    Protected Sub imagemanager_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

        imagemanagerurl.Text = "<img src='gallery/" & imagemanagerdropdown.SelectedValue.ToString & "'>"

        'Dim image1 As Bitmap

        image1 = New Bitmap("D:\gallery\" & imagemanagerdropdown.SelectedValue.ToString, True)

        imgwidth.Text = "Width: " & image1.Width
        imgheight.Text = "Height: " & Image1.Height

        Image1.Dispose()

        UpdatePanelImageManager.Update()

The Image1.Dispose() was not there when I was getting the error in the following code.

Code:
    Protected Sub godeleteimage_Click(ByVal sender As Object, ByVal e As EventArgs) Handles godeleteimage.Click

        If deleteimage.Checked = True Then

            Try

                'Dim ImageName = "test1thumb.png"
                Dim ImageName = imagemanagerdropdown.SelectedValue.ToString

                Dim ImagePath As [String] = [String].Format("{0}\{1}", Server.MapPath("gallery"), ImageName)

                If System.IO.File.Exists(ImagePath) Then
                    System.IO.File.Delete(ImagePath)
                End If

            Catch ex As Exception
                imagemanagerurl.Text = ErrorToString()
            End Try

        End If

        UpdatePanelImageManager.Update()


    End Sub

Once the bitmap was being disposed by adding the Image1.Dispose(), my image delete functionality works great.

mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top