Home All Groups Group Topic Archive Search About

Deleting a Photo stored in a Picturebox

Author
13 Jul 2006 8:17 PM
TomA
Hi All,

I have a picturebox on a form containing the photo of a person.  As you
advance through the records, the photo updates.  Rather than storing the
images in an inefficient blob field in a table, I have the separate images
stored in an image directory with primary key as the filename (3476.jpg).  If
the image exists, I display the image.  All this works fine.

I have a buttons that allow for the insertion and deletion of photos.  The
“add” works fine too.  But I am getting errors when I attempt to delete an
image – “file is being used by another process.”  The only time the image is
“used” is when I display it on the form.  I am assuming the form/picturebox
is locking the image (what else?).  I coded the procedure to reset the image
to a default missing image photo.  That did not work so I set the picturebox
to nothing first.  That did not work either.  I also verified the string
being passed to the delete.file method.  Here is my code:


    Private Sub cmdRemovePhoto_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles cmdRemovePhoto.Click

        Dim intReturn As Integer

        intReturn = MsgBox("Delete this Photo?  This cannot be reversed.",
MsgBoxStyle.YesNo + MsgBoxStyle.Question + MsgBoxStyle.DefaultButton2,
"Warning...")

        If intReturn = 6 Then

            picPhoto.Image = Nothing

            picPhoto.Image = Image.FromFile(pubGlobalSetup &
"\imageFolder\missingphoto.jpg")

            Try

                File.Delete(pubGlobalSetup & "\imageFolder\" &
LTrim(Str(intContactID)) & ".jpg") ' <--Error Here

                cmdRemovePhoto.Enabled = False

            Catch ee As Exception

                MsgBox(ee.Message)

            End Try

        End If

    End Sub



I would sure appreciate any ideas.  This should be easy.   I have no clue
what could be locking the file.  It has to be the picturebox doesn’t it? But
at the time I am deleting, it has been replaced.  I checked the file
attribues at the time I delete and it is set only to "Archive".  Someone in
Google had a similar problem and the respondant suggested a file.close
method.  There is no such method.  There is a fileclose(buffer#) method for
datafiles.

Thanks much!
--
Tom

Author
13 Jul 2006 9:56 PM
tommaso.gastaldi
Try loading. Making a in memory copy of the image (drawimage). Dispose
original Image used for load.
Let me know...

-tom

TomA ha scritto:

Show quoteHide quote
> Hi All,
>
> I have a picturebox on a form containing the photo of a person.  As you
> advance through the records, the photo updates.  Rather than storing the
> images in an inefficient blob field in a table, I have the separate images
> stored in an image directory with primary key as the filename (3476.jpg).  If
> the image exists, I display the image.  All this works fine.
>
> I have a buttons that allow for the insertion and deletion of photos.  The
> "add" works fine too.  But I am getting errors when I attempt to delete an
> image - "file is being used by another process."  The only time the image is
> "used" is when I display it on the form.  I am assuming the form/picturebox
> is locking the image (what else?).  I coded the procedure to reset the image
> to a default missing image photo.  That did not work so I set the picturebox
> to nothing first.  That did not work either.  I also verified the string
> being passed to the delete.file method.  Here is my code:
>
>
>     Private Sub cmdRemovePhoto_Click(ByVal sender As System.Object, ByVal e
> As System.EventArgs) Handles cmdRemovePhoto.Click
>
>         Dim intReturn As Integer
>
>         intReturn = MsgBox("Delete this Photo?  This cannot be reversed.",
> MsgBoxStyle.YesNo + MsgBoxStyle.Question + MsgBoxStyle.DefaultButton2,
> "Warning...")
>
>         If intReturn = 6 Then
>
>             picPhoto.Image = Nothing
>
>             picPhoto.Image = Image.FromFile(pubGlobalSetup &
> "\imageFolder\missingphoto.jpg")
>
>             Try
>
>                 File.Delete(pubGlobalSetup & "\imageFolder\" &
> LTrim(Str(intContactID)) & ".jpg") ' <--Error Here
>
>                 cmdRemovePhoto.Enabled = False
>
>             Catch ee As Exception
>
>                 MsgBox(ee.Message)
>
>             End Try
>
>         End If
>
>     End Sub
>
>
>
> I would sure appreciate any ideas.  This should be easy.   I have no clue
> what could be locking the file.  It has to be the picturebox doesn't it? But
> at the time I am deleting, it has been replaced.  I checked the file
> attribues at the time I delete and it is set only to "Archive".  Someone in
> Google had a similar problem and the respondant suggested a file.close
> method.  There is no such method.  There is a fileclose(buffer#) method for
> datafiles.
>
> Thanks much!
> --
> Tom
Author
13 Jul 2006 11:57 PM
TomA
Thanks Tom (1),

Your suggestion worked.  I had to make several changes in the program.  In
the event someone else has the same problem and is searching, I loaded the
image clone into the picturebox using the following code:

          Private m_Bitmap As Bitmap ' Form level

            Dim bm As New Bitmap(ImagefileName)
            m_Bitmap = New Bitmap(bm.Width, bm.Height)
            Dim gr As Graphics = Graphics.FromImage(m_Bitmap)
            gr.DrawImage(bm, 0, 0)
            bm.Dispose()
            picPhoto.Image = m_Bitmap

The file was no longer locked - the picturebox contained a clone and I
disposed of the original file from memory.  I was free to delete the file
without a file locking challenge.

Take care!

Tom (2)
--
Tom


Show quoteHide quote
"tommaso.gasta***@uniroma1.it" wrote:

> Try loading. Making a in memory copy of the image (drawimage). Dispose
> original Image used for load.
> Let me know...
>
> -tom
>
> TomA ha scritto:
>
> > Hi All,
> >
> > I have a picturebox on a form containing the photo of a person.  As you
> > advance through the records, the photo updates.  Rather than storing the
> > images in an inefficient blob field in a table, I have the separate images
> > stored in an image directory with primary key as the filename (3476.jpg).  If
> > the image exists, I display the image.  All this works fine.
> >
> > I have a buttons that allow for the insertion and deletion of photos.  The
> > "add" works fine too.  But I am getting errors when I attempt to delete an
> > image - "file is being used by another process."  The only time the image is
> > "used" is when I display it on the form.  I am assuming the form/picturebox
> > is locking the image (what else?).  I coded the procedure to reset the image
> > to a default missing image photo.  That did not work so I set the picturebox
> > to nothing first.  That did not work either.  I also verified the string
> > being passed to the delete.file method.  Here is my code:
> >
> >
> >     Private Sub cmdRemovePhoto_Click(ByVal sender As System.Object, ByVal e
> > As System.EventArgs) Handles cmdRemovePhoto.Click
> >
> >         Dim intReturn As Integer
> >
> >         intReturn = MsgBox("Delete this Photo?  This cannot be reversed.",
> > MsgBoxStyle.YesNo + MsgBoxStyle.Question + MsgBoxStyle.DefaultButton2,
> > "Warning...")
> >
> >         If intReturn = 6 Then
> >
> >             picPhoto.Image = Nothing
> >
> >             picPhoto.Image = Image.FromFile(pubGlobalSetup &
> > "\imageFolder\missingphoto.jpg")
> >
> >             Try
> >
> >                 File.Delete(pubGlobalSetup & "\imageFolder\" &
> > LTrim(Str(intContactID)) & ".jpg") ' <--Error Here
> >
> >                 cmdRemovePhoto.Enabled = False
> >
> >             Catch ee As Exception
> >
> >                 MsgBox(ee.Message)
> >
> >             End Try
> >
> >         End If
> >
> >     End Sub
> >
> >
> >
> > I would sure appreciate any ideas.  This should be easy.   I have no clue
> > what could be locking the file.  It has to be the picturebox doesn't it? But
> > at the time I am deleting, it has been replaced.  I checked the file
> > attribues at the time I delete and it is set only to "Archive".  Someone in
> > Google had a similar problem and the respondant suggested a file.close
> > method.  There is no such method.  There is a fileclose(buffer#) method for
> > datafiles.
> >
> > Thanks much!
> > --
> > Tom
>
>
Author
14 Jul 2006 12:29 AM
tommaso.gastaldi
It is also nice Herfried's suggestion (the second one) based on
streams.

If you do not need to resize the image, it would be interesting
to measure which one is faster, to use DrawImage or the stream.
I suspect that the second way might be faster.

If you try it out let us know!

-tom (1) :)

TomA ha scritto:

Show quoteHide quote
> Thanks Tom (1),
>
> Your suggestion worked.  I had to make several changes in the program.  In
> the event someone else has the same problem and is searching, I loaded the
> image clone into the picturebox using the following code:
>
>           Private m_Bitmap As Bitmap ' Form level
>
>             Dim bm As New Bitmap(ImagefileName)
>             m_Bitmap = New Bitmap(bm.Width, bm.Height)
>             Dim gr As Graphics = Graphics.FromImage(m_Bitmap)
>             gr.DrawImage(bm, 0, 0)
>             bm.Dispose()
>             picPhoto.Image = m_Bitmap
>
> The file was no longer locked - the picturebox contained a clone and I
> disposed of the original file from memory.  I was free to delete the file
> without a file locking challenge.
>
> Take care!
>
> Tom (2)
> --
> Tom
>
>
> "tommaso.gasta***@uniroma1.it" wrote:
>
> > Try loading. Making a in memory copy of the image (drawimage). Dispose
> > original Image used for load.
> > Let me know...
> >
> > -tom
> >
> > TomA ha scritto:
> >
> > > Hi All,
> > >
> > > I have a picturebox on a form containing the photo of a person.  As you
> > > advance through the records, the photo updates.  Rather than storing the
> > > images in an inefficient blob field in a table, I have the separate images
> > > stored in an image directory with primary key as the filename (3476.jpg).  If
> > > the image exists, I display the image.  All this works fine.
> > >
> > > I have a buttons that allow for the insertion and deletion of photos.  The
> > > "add" works fine too.  But I am getting errors when I attempt to delete an
> > > image - "file is being used by another process."  The only time the image is
> > > "used" is when I display it on the form.  I am assuming the form/picturebox
> > > is locking the image (what else?).  I coded the procedure to reset the image
> > > to a default missing image photo.  That did not work so I set the picturebox
> > > to nothing first.  That did not work either.  I also verified the string
> > > being passed to the delete.file method.  Here is my code:
> > >
> > >
> > >     Private Sub cmdRemovePhoto_Click(ByVal sender As System.Object, ByVal e
> > > As System.EventArgs) Handles cmdRemovePhoto.Click
> > >
> > >         Dim intReturn As Integer
> > >
> > >         intReturn = MsgBox("Delete this Photo?  This cannot be reversed.",
> > > MsgBoxStyle.YesNo + MsgBoxStyle.Question + MsgBoxStyle.DefaultButton2,
> > > "Warning...")
> > >
> > >         If intReturn = 6 Then
> > >
> > >             picPhoto.Image = Nothing
> > >
> > >             picPhoto.Image = Image.FromFile(pubGlobalSetup &
> > > "\imageFolder\missingphoto.jpg")
> > >
> > >             Try
> > >
> > >                 File.Delete(pubGlobalSetup & "\imageFolder\" &
> > > LTrim(Str(intContactID)) & ".jpg") ' <--Error Here
> > >
> > >                 cmdRemovePhoto.Enabled = False
> > >
> > >             Catch ee As Exception
> > >
> > >                 MsgBox(ee.Message)
> > >
> > >             End Try
> > >
> > >         End If
> > >
> > >     End Sub
> > >
> > >
> > >
> > > I would sure appreciate any ideas.  This should be easy.   I have no clue
> > > what could be locking the file.  It has to be the picturebox doesn't it? But
> > > at the time I am deleting, it has been replaced.  I checked the file
> > > attribues at the time I delete and it is set only to "Archive".  Someone in
> > > Google had a similar problem and the respondant suggested a file.close
> > > method.  There is no such method.  There is a fileclose(buffer#) method for
> > > datafiles.
> > >
> > > Thanks much!
> > > --
> > > Tom
> >
> >
Author
13 Jul 2006 11:01 PM
Herfried K. Wagner [MVP]
"TomA" <T***@discussions.microsoft.com> schrieb:
> I have a buttons that allow for the insertion and deletion of photos.  The
> “add” works fine too.  But I am getting errors when I attempt to delete an
> image – “file is being used by another process.”  The only time the image
> is
> “used” is when I display it on the form.  I am assuming the
> form/picturebox
> is locking the image (what else?).

Check out the snippets in the article below -- they demonstrate ways to load
an image without locking the file:

<URL:http://dotnet.mvps.org/dotnet/code/graphics/#ImageNoLock>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>