By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,426 Members | 2,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

Deleting a Photo stored in a Picturebox

P: n/a
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
Jul 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Jul 13 '06 #2

P: n/a
"TomA" <To**@discussions.microsoft.comschrieb:
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/>

Jul 13 '06 #3

P: n/a
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
"to**************@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

Jul 13 '06 #4

P: n/a
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:
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
"to**************@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
Jul 14 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.