Delete Image
Delete Image
Delete Image
Delete Image
Delete Image
Delete Image Delete Image Delete Image Delete Image Delete Image Delete Image Delete Image Delete Image
Delete Image Delete Image
Delete Image
Go Back  Xtreme Visual Basic Talk > > > Delete Image


Reply
 
Thread Tools Display Modes
  #1  
Old 07-21-2013, 10:03 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default Delete Image


I have created a database program that stores the location of image files among other data.
It an access DB using OLEDB connection.

Wasn't sure where to ask this question because the database appears to be working just fine.
My problem begins when i try to delete an image file, from a folder.

If i delete a record, i want to be able to delete the physical image
file as well but when i do, i get this error:


Quote:
The process can't access the file because it is being used by another process.
I went through all the code and made sure everything that was opened, was closed, so there shouldn't be any open processes still holding that image file location in memory.

In fact, when i delete the record, everything including the image file location, gets deleted, so why can't i delete the physicle image file.
It wasn't even put in the database, just it's location was and it was deleted.

I can't even manually, go to the folder that holds the image file and delete it that way, unless i close down my program first.

So i realize that something in my program is still holding that image file location in memory but what?

Hope i don't come back here, 5 mins later and answer my own question. LOL

Last edited by Flyguy; 07-22-2013 at 01:14 AM.
Reply With Quote
  #2  
Old 07-22-2013, 12:56 AM
Gruff's Avatar
GruffDelete Image Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

I've written a complete file control systems from scratch. I ran into this on several occasions. Basically Windows is generating the error. I think I forced the default folder to point to some other folder... Like the root before I moved or deleted files.

Stepping through the app from launch to delete might shed some light.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #3  
Old 07-22-2013, 02:13 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

That is an odd thing to do but i'll
give it a shot.
Reply With Quote
  #4  
Old 07-22-2013, 02:31 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

You are right, in that it is a system windows error
popping up and not a vb error.

For some reason, windows is not releasing the information
about the image location.

I did change the working directory to C: before trying
to delete the physical image file but i still get the same
error.

I'm at a loss as to what to do. I made sure anything that i
opened like a db connection is closed and disposed of any
datatables created. I closed out any oledb readers and anything
else that could be closed or disposed of, before calling my deleteimage
sub. Still get the same error though and the image is still left
in the folder.

Just by chance, maybe i'm not using this code right but i looks
right to me.

Code:
Private Sub DeleteImage(FileName As String)

        Try

            My.Computer.FileSystem.CurrentDirectory = "C:\"
            My.Computer.FileSystem.DeleteFile(FileName)

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
I'm also using my computer filesystem to copy
images from one folder to another.

Do you think that the use of the filesystem is
what is causing my problems?

I don't think there is a way to dispose of that
or set it to nothing or close it down.

Does the filesystem release it data from memory
when not in use?

Last edited by VB_Alien; 07-22-2013 at 02:51 PM.
Reply With Quote
  #5  
Old 07-22-2013, 03:58 PM
Gruff's Avatar
GruffDelete Image Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Are you trying to delete a file immediately after copying or moving it?

If that is the case then There are definite timing issues as to how long some file process take to complete. Deletion can fail if they are not done.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 07-22-2013 at 04:08 PM.
Reply With Quote
  #6  
Old 07-22-2013, 04:57 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

To answer you question, yes i was deleting
the picture, soon after adding it to the database.

One should be able to do that, no matter how
long the file has been there.

I even shut down my program, restarted it and
tried to delete the file again and i still had the
same error.

Well anyway, i believe i found the offending code.
It's a sub that i made that reads all the data into
textboxes and a variable for the image location.

Then i use, image.fromfile to load the image into
a picturebox.

What i did was put exit sub at the very top of my
sub, so that no code would be executed in it.

Then i ran my program again and added a new record,
including the image file.

The image file was moved to it's new folder properly
like before and i turned around and deleted it right away
and it got deleted.

The only problem is, that sub loads at the data into textboxes
and without that code working, i can't see my data.

I'll post my sub code below. Maybe you can tell me what
i'm doing wrong and suggest a different approach to loading
data into my textboxes.

Code:
 Public Sub ShowRecipes(TableName As String, RecipeName As String)

        ' Connection string for my database
        Dim Connection As OleDbConnection = New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0; Data Source=" & db_name & ";")

        ' String to tell database to drop or delete a table
        Dim strQuery As String = "SELECT * FROM " & TableName & " Where Recipe_Name ='" & RecipeName & "'"

        'create a command
        Dim my_Command As New OleDbCommand(strQuery, Connection)

        Connection.Open()
        Dim read As OleDbDataReader = my_Command.ExecuteReader()
        Dim ImgPath As String = ""

        Try

            ClearFields()

            If read.HasRows Then
                read.Read()

                ' Now you can read the Item and pass the values to your textboxes             
                txtRecipeName.Text = read.Item("Recipe_Name").ToString
                rtbIngredients.Rtf = read.Item("Ingredients").ToString
                rtbPrep.Rtf = read.Item("Prep").ToString
                txtNotes.Text = read.Item("Notes").ToString
                ImgPath = read.Item("Picture").ToString

                read.Close()

                If ImgPath > "" Then
                    picFood.Image = Image.FromFile(ImgPath)
                Else
                    ' Do nothing
                End If
            End If

            my_Command.Dispose()
            Connection.Close()
        Catch ex As Exception
            MsgBox(ex.Message)

            read.Close()
            my_Command.Dispose()
            Connection.Close()
        End Try

    End Sub
Reply With Quote
  #7  
Old 07-22-2013, 11:50 PM
Gruff's Avatar
GruffDelete Image Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

try commenting out only:

picFood.Image = Image.FromFile(ImgPath)

Does your app allow the image file to be deleted?
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #8  
Old 07-23-2013, 08:49 AM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

I did that and it deleted the image, so it is
the one piece of offending code.

The thing is, if that is the offending code, how
should i display my images?

I was wondering if an ADO database, using
recordsets wouldn't be more suited for this
job.

Since i'm only using text as data, the database
shouldn't become to bloated very fast.

I am using large chunks of text though like
recipe "Ingredients", "Prep Instructions" and
"Notes". Currently, they are being used in the
database as memo's.

I assume a recordset can hold large chunks of
text though.
Reply With Quote
  #9  
Old 07-23-2013, 09:07 AM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

I've discovered that it is the act of reading
the image file location into a variable that
is causing the problem.

ImgPath = read.Item("Picture").ToString ' offending code

When i don't comment out this code, i get the error
but when i do comment it out, i can delete the image
right away.

The thing is, my images never display in the picturebox.

ADO is looking pretty good right now. LOL
Reply With Quote
  #10  
Old 07-23-2013, 09:18 AM
Gruff's Avatar
GruffDelete Image Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

The good news is that now that you have zeroed in on the line of code that is the issue you can do much more pointed internet searches for others with like problems.

Here is a solution from Microsoft
http://support.microsoft.com/kb/309482/en-us


Here is a different soution i found in german. Haven't tested it but you can try.

Code:
Dim OriginalImage As New Bitmap("C:\WINDOWS\Angler.bmp") Dim Image As New Bitmap(OriginalImage) OriginalImage.Dispose() . . . Image.Dispose()


I'd create an ImageEx class library to contain the fix in a nice wrapper.
You could store other custom image routines in it as well.
Reference the library dll in any project. Bingo.


Regarding your database... I've avoided the datareader classes in favor of reading a datatable and handling the records from that. Basically I create a dataclass that does the grunt work. My ReadDB function returns a dataview which I can filter on the fly after my query does it's job. (If I need to.) How many records in your table?
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 07-23-2013 at 09:48 AM.
Reply With Quote
  #11  
Old 07-23-2013, 02:24 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

As you may have guessed by now, i'm
creating a recipe book. It has several
tables in it and some tables have several
records in it.

Right now, i'm using an empty table to
create a new recipe record in it and i'm
adding the image location to it, just like
i did with all the other one.

I'm using the empty table to test on, so that
i don't mess up my other data.

I went to the ms link that you provided but
from what i can see, that won't work either.

It's the act of read the image location, is what
is being held in memory. Even after the reader
is close, the data is still in the windows system
and as long as it is there, windows won't allow
me access to the physical image file, either,
programically or manually.

I think i need to find a different way to extract
information from just one record at a time.

I'll try working with datatables and see what happens.

I got side stepped there for a min and was toying with
the idea of going back to DAO. Boy is that thing ancient.
I'm over it now. LOL
Reply With Quote
  #12  
Old 07-23-2013, 03:01 PM
Gruff's Avatar
GruffDelete Image Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

If you are talking about the older ADO that you used in VB6 then I do not understand why you would use it. ADO.NET with VB.NET can do everything the the old ADO object could do and more.

The DataReader you used in your code is only a very small part of the package.

RE: file locking... Did a test and this works for me

If I remove the dispose call the file will not delete.
Add it and I can delete the file.

Code:
Public Class Form1 Private Sub cmdLoad_Click(sender As System.Object, e As System.EventArgs) Handles cmdLoad.Click 'Add test image for P1 P1.BackgroundImageLayout = ImageLayout.Stretch P1.BackgroundImage = Image.FromFile("C:\AAA\Koala.jpg") End Sub Private Sub cmdDelete_Click(sender As System.Object, e As System.EventArgs) Handles cmdDelete.Click P1.BackgroundImage.Dispose() System.IO.File.Delete("C:\AAA\Koala.jpg") 'Remove from database here. End Sub End Class
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 07-23-2013 at 04:07 PM.
Reply With Quote
  #13  
Old 07-23-2013, 06:01 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

Tried your code and i got the same problem +
a new one.

When i dispose of the picImage.Image, i get
a red X painted in the picturebox. That is why
i never dispose of pictureboxes like that.

Anyway, i changed my code to work off of a dataset
instead of reading in the values and i still have the same
problem. Here is the new code.

Code:
 Public Sub ShowRecipes(TableName As String, RecipeName As String)

        Dim ds As New DataSet

        ' String to tell database to drop or delete a table
        Dim strQuery As String = "SELECT * FROM " & TableName & " Where Recipe_Name ='" & RecipeName & "'"

        ' Connection string for my database
        Dim Connection As OleDbConnection = New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0; Data Source=" & db_name & ";")

        Connection.Open()

        Dim Filler As OleDbDataAdapter = New OleDbDataAdapter(strQuery, Connection)

        Try

            Filler.Fill(ds)

            txtRecipeName.Text = ds.Tables(0).Rows(0).Item("Recipe_Name").ToString
            rtbIngredients.Rtf = ds.Tables(0).Rows(0).Item("Ingredients").ToString
            rtbPrep.Rtf = ds.Tables(0).Rows(0).Item("Prep").ToString
            txtNotes.Text = ds.Tables(0).Rows(0).Item("Notes").ToString

            sImageName = ds.Tables(0).Rows(0).Item("Picture").ToString

            picImage.BackgroundImageLayout = ImageLayout.Stretch
            picImage.BackgroundImage = Image.FromFile(sImageName)

            Filler.Dispose()
            ds.Dispose()

            Connection.Close()

        Catch ex As Exception
            ' MsgBox(ex.Message)

            ' Had to comment out my error message
            ' It's saying that there is no row
            ' at position 0.

            ' Not sure what to do 

            Filler.Dispose()
            ds.Dispose()

            '  my_Command.Dispose()
            Connection.Close()
        End Try

    End Sub
Reply With Quote
  #14  
Old 07-23-2013, 06:04 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

Just a thought but maybe i should just
put the picture inside the database. I
wouldn't have to worry about deleting
images outside the database then.

I might do that.. Everything else is working fine.
Reply With Quote
  #15  
Old 07-24-2013, 07:46 AM
passel's Avatar
passelDelete Image passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Quote:
Originally Posted by VB_Alien View Post
...
When i dispose of the picImage.Image, i get
a red X painted in the picturebox. That is why
i never dispose of pictureboxes like that.
...
Just a heads up, if you dispose the image a reference is pointing to (and you don't assign a new image), then you need to remove the reference. The red cross in the picturebox indicates you have a reference to a non-existing object.
Code:
'
    PictureBox1.BackgroundImage.Dispose()  'get rid of the image resources
    PictureBox1.BackgroundImage = Nothing  'remove the reference
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #16  
Old 07-24-2013, 10:30 AM
Gruff's Avatar
GruffDelete Image Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Interesting. I do not get a red X when I use backgroundimage.dispose.
I am not reading my path from a database just a hard coded string.

BTW What version of VB.NET are you using? What service pack?

--- Here is my setup. ---

Microsoft Visual Studio 2010
Version 10.0.40219.1 SP1Rel
Microsoft .NET Framework
Version 4.5.50709 SP1Rel

Installed Version: Professional
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 07-24-2013 at 10:44 AM.
Reply With Quote
  #17  
Old 07-24-2013, 01:39 PM
passel's Avatar
passelDelete Image passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Did you cause a paint event for the picturebox,e.g. drag something over the picturebox to cause a paint event after doing the dispose?
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 07-24-2013 at 01:51 PM.
Reply With Quote
  #18  
Old 07-24-2013, 03:30 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

Quote:
Originally Posted by passel View Post
Did you cause a paint event for the picturebox,e.g. drag something over the picturebox to cause a paint event after doing the dispose?
When i delete an image file location, a listbox that contains
a list of all tables in the database, sort of gets refreshed.

Since i would have just deleted the only record in that table,
there is no more records to select from, so i don't know how
my picturebox would repaint with nothing there to repaint.

Still though, even if i try to manually go to the folder containing
the picture file and try to delete it, while my program is still
running, i can't delete it that way either.

The windows system is holding onto the picture file location
and won't release it, unless i close down my program.

I probably forgot to mention this part and i'm sorry for that
but the picture in my picturebox does get removed because
i call a "ClearFields" sub that clears out all the fields in a
record.

Here is that sub
Code:
 Private Sub ClearFields()
        rtbPrep.Clear()
        rtbIngredients.Clear()
        txtRecipeName.Clear()
        txtNotes.Clear()
        picImage.BackgroundImage.Dispose()
        picImage.BackgroundImage = Nothing
    End Sub
I tried it with your suggestion as you can see and the
image get removed as before but windows system is
still preventing me access to that image file.

At least there is no more red x painted in the
picturebox though.

I'm at a loss but i'm disposing of everything i can
see that would cause this problem and still nothing
is working.
Reply With Quote
  #19  
Old 07-24-2013, 03:36 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

Gruff

I'm using

Microsost Visual Studio 2010 pro
Version 10.0.40219.1 SP1
Dot Net Framework 4.0 SP1

Windows 7
Reply With Quote
  #20  
Old 07-25-2013, 10:30 AM
Gruff's Avatar
GruffDelete Image Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Let's try to do a further test.
Start a simple empty test project. Put two buttons on it and a picturebox.
Name the controls as in my sample below.
Paste my code into your form.

If this test works then you know the issue is elsewhere with your original project.

If it does not work then we know that there is a fundamental difference between your system and mine. Perhaps a Windows update pack.

Code:
Public Class Form1 Private Sub cmdLoad_Click(sender As System.Object, e As System.EventArgs) Handles cmdLoad.Click 'Add test image for P1 P1.BackgroundImageLayout = ImageLayout.Stretch ' Change the file to one on your system. P1.BackgroundImage = Image.FromFile("C:\AAA\Koala.jpg") End Sub Private Sub cmdDelete_Click(sender As System.Object, e As System.EventArgs) Handles cmdDelete.Click P1.BackgroundImage.Dispose() P1.BackgroundImage = Nothing ' Change the file to one on your system. System.IO.File.Delete("C:\AAA\Koala.jpg") End Sub End Class
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Delete Image
Delete Image
Delete Image Delete Image
Delete Image
Delete Image
Delete Image Delete Image Delete Image Delete Image Delete Image Delete Image Delete Image
Delete Image
Delete Image
 
Delete Image
Delete Image
 
-->