473,757 Members | 3,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.EventArg s) Handles cmdRemovePhoto. Click

Dim intReturn As Integer

intReturn = MsgBox("Delete this Photo? This cannot be reversed.",
MsgBoxStyle.Yes No + MsgBoxStyle.Que stion + MsgBoxStyle.Def aultButton2,
"Warning... ")

If intReturn = 6 Then

picPhoto.Image = Nothing

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

Try

File.Delete(pub GlobalSetup & "\imageFold er\" &
LTrim(Str(intCo ntactID)) & ".jpg") ' <--Error Here

cmdRemovePhoto. Enabled = False

Catch ee As Exception

MsgBox(ee.Messa ge)

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(buffe r#) method for
datafiles.

Thanks much!
--
Tom
Jul 13 '06 #1
4 2008
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.EventArg s) Handles cmdRemovePhoto. Click

Dim intReturn As Integer

intReturn = MsgBox("Delete this Photo? This cannot be reversed.",
MsgBoxStyle.Yes No + MsgBoxStyle.Que stion + MsgBoxStyle.Def aultButton2,
"Warning... ")

If intReturn = 6 Then

picPhoto.Image = Nothing

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

Try

File.Delete(pub GlobalSetup & "\imageFold er\" &
LTrim(Str(intCo ntactID)) & ".jpg") ' <--Error Here

cmdRemovePhoto. Enabled = False

Catch ee As Exception

MsgBox(ee.Messa ge)

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(buffe r#) method for
datafiles.

Thanks much!
--
Tom
Jul 13 '06 #2
"TomA" <To**@discussio ns.microsoft.co mschrieb:
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(Imagefil eName)
m_Bitmap = New Bitmap(bm.Width , bm.Height)
Dim gr As Graphics = Graphics.FromIm age(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.EventArg s) Handles cmdRemovePhoto. Click

Dim intReturn As Integer

intReturn = MsgBox("Delete this Photo? This cannot be reversed.",
MsgBoxStyle.Yes No + MsgBoxStyle.Que stion + MsgBoxStyle.Def aultButton2,
"Warning... ")

If intReturn = 6 Then

picPhoto.Image = Nothing

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

Try

File.Delete(pub GlobalSetup & "\imageFold er\" &
LTrim(Str(intCo ntactID)) & ".jpg") ' <--Error Here

cmdRemovePhoto. Enabled = False

Catch ee As Exception

MsgBox(ee.Messa ge)

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(buffe r#) 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(Imagefil eName)
m_Bitmap = New Bitmap(bm.Width , bm.Height)
Dim gr As Graphics = Graphics.FromIm age(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.EventArg s) Handles cmdRemovePhoto. Click
>
Dim intReturn As Integer
>
intReturn = MsgBox("Delete this Photo? This cannot be reversed.",
MsgBoxStyle.Yes No + MsgBoxStyle.Que stion + MsgBoxStyle.Def aultButton2,
"Warning... ")
>
If intReturn = 6 Then
>
picPhoto.Image = Nothing
>
picPhoto.Image = Image.FromFile( pubGlobalSetup &
"\imageFolder\m issingphoto.jpg ")
>
Try
>
File.Delete(pub GlobalSetup & "\imageFold er\" &
LTrim(Str(intCo ntactID)) & ".jpg") ' <--Error Here
>
cmdRemovePhoto. Enabled = False
>
Catch ee As Exception
>
MsgBox(ee.Messa ge)
>
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(buffe r#) 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
1197
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 same file name plus the suffix "_small" so if the main file is called "mypicture.jpg" the thumbnail is called "mypicture_small.jpg"
2
1974
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 information of the photo and set up a folder to store the "PHOTOs". and design a web page to show. My question is how to put the photo on the web page.
3
5923
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 "graphics" folder that is on the same level as the "xyz" folder. The only way I can seem to make it work is to put the entire path including hard drive in the table (c:\database\graphics\photo.jpg). But I also want to upload the database to the web...
16
3098
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 which deletes the product record from the database, however the product image remains in the associated folder on the server. I'd prefer these images not accumulate and would like to delete these images along with the database record. Can...
5
2114
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. On my Web Page, I have a button named delete which I used to delete photo in the folder. But when I click the button and apply the logic to delete the file, it raises an Exception that the access to the file is denied. I am using following code to...
4
1986
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 one by one to PictureBox control and display it when the Timer control ticks. Coding like this: ------------------------------------------------------ IList<Imagephotoes = new List<Image>();...
6
15091
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 is being used by other process. Is there a way to "free" the file? Thank you for your help.
4
1750
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. I do this by using the image.fromfile function: pbProduct.Image = Image.FromFile(myImagePath & productkey & ".jpg") Now I'm building the option to delete a product. When a product is selected
5
23916
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 delete the loaded image file from the directory. But I have a problem deleting the image file used by the picturebox even if I set the "PictureBox1.Image = Nothing" before deletion takes place. The error says, "The process cannot access the file...
0
9487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6556
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.