473,385 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Deleting a Photo stored in a Picturebox

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
4 1989
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Me | last post by:
Hi Folks, I'm trying to delete 2 files using fso. The mail file name is stored in an access db. using the code below I can delete the main file but not the thumbnail. The thumbnail has the...
2
by: Tony WONG | last post by:
i am not sure that this subject can be discussed here. i have many photos. they are stored according to the name of the EVENT and YEAR. i will set up a database (sql or access) to store...
3
by: Ken | last post by:
I have a database called autographs.mdb that is in the "XYZ" folder in the "database" folder. I have a form in the database that I want to display a photo of the celeb on. The photos are in a...
16
by: Frankie | last post by:
Hello: I'm coding a website which allows the owner to make add/change/deletes to his product database, which includes product images stored separately on the server. I have coded the Delete page...
5
by: Sandeep Singh Sekhon | last post by:
I am Developing a Web Application with ASP.NET 1.1 I have one project Folder which is virtual directory of IIS. In this directory, I have one Folder named Photos in which I used to Store Photos....
4
by: Mike Chen | last post by:
Dear all, I made a c# program for my friend's wedding. It slides all photos in specified folder automatically. To roll the pictures smoothly I firstly scan and store all pictures in RAM and push...
6
by: FrankB | last post by:
Hello, after setting another image to the picture box it is not possible to delete the last shown file via File.Delete ( sFilename). Path of image file is correct. Error message box says: file...
4
by: Bart Steur | last post by:
Hi, I'm writing an app to maintain products. The products are listed in a listbox and when I click a product in a listbox some info of that product is shown including a picture of the product. ...
5
krungkrung
by: krungkrung | last post by:
hi again to everyone! I made a simple program(for my VB.Net practice). The program loads an image file to a picturebox upon clicking a button. after loading the image file i have another button to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.