Temp Image file is locked and cannot delete

Hello,

I just can't seem to get this to work right for some reason. I am getting pretty frustrated and I am sure it is an easy fix. I have the following line of code:

Dim pictForm As Image = Image.FromFile(strFilenameJPG)

In the next couple of lines, I draw the image into my graphics object. The strFilenameJPG varaible is just a temp file. When I have completed my grahpics work, I want to delete the file. I use the following line:

Kill (strFilenameJPG)

However, I get an I/O error every time saying that the file is in use. I have tried several methods such as disposing the pictForm object and setting it equal to nothing, but I always get the same error.

Any help would be appreciated.

Tom




Answer this question

Temp Image file is locked and cannot delete

  • Tey Boon Kiat

    Thanks for the reply.

    I tried both of your solutions. I rebooted just to see if there was some weird hanging issue. But that didn't do any thing different. I already have full administrator access on this PC.

    The problem is only temporary. The temp image file that my visual basic program creates is locked by my program. If I close my program, then I can delete the file just fine through traditional means.

    However, I want my program to clean up after itself programmatically. I want to delete the temp file within the program after I have incorporated it into my graphics object. It is when I attempt this process in my visual basic program that I get the I/O error.

    Thanks,

    Tom



  • FranklinBAH

    Ah, so when you close your program the file can be deleted, I thought it is still locked even after exiting.

    In this case try the following (works 100% - you just need to destroy the object):

    Module Module1

    Sub Main()

    Dim pictfrm As Drawing.Image = Drawing.Image.FromFile("c:\asd.jpg")

    pictfrm.Dispose()

    IO.File.Delete("c:\asd.jpg")

    End Sub

    End Module



  • Nimble

    Hello:

    I have the same problem. I can't find anyway to fix.

    Help appreciated.

    Carlos



  • Kent Boogaart

    I think it's a windows issue. Try rebooting, if that doesn't help you can go to security tab in file properties and remove all accounts from there and then add yourself with full control and then try deleting. I have had these problems, I am quite sure it's not with VB.

    Instead of kill (which I never heard about before) you can try using system.IO.File.delete or the fileinfo class in the same namespace.


  • Jeremy Corson

    Thanks again for the reply.

    Unfortunately, the code that you posted above is exactly what I am doing in my sub routine. Only I am getting the I/O error when the IDE gets to the Delete File command. It seems as though the dispose command isn't really freeing the JPG file.

    Any other ideas

    Tom



  • Ravindra Reddy Kasireddy

    I had the same problem a few days ago while implementing a twain scanner dialog. The problem lies with the bitmap / image object locking the file until application is closed.

    This is the code I used to get around the issue

     

    Dim myimage As Image

    Dim myimagestream As New IO.FileStream(filename, IO.FileMode.Open)

    myimage = Bitmap.FromStream(myimagestream)

    myimagestream.Close()

    myimagestream.Dispose()

     


  • Ritesh Singh

    I found out what the problem is. One of the references I was using in my project was a library for PDF creation. This library also has an image object, but their seems to be a problem with the library's dispose function.

    Anyhow, one I specified to use the drawing image object, everything worked out fine.

    Thanks for the help.

    Tom



  • Temp Image file is locked and cannot delete