473,396 Members | 1,779 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,396 software developers and data experts.

PictureBox "locks" file, cannot move/delete

Any help appreciated.

(VB.NET under XP and Vista, SP1 installed)

My code, inherited from a VB6 version of an app that ran under W98,
loads an image from a file into a PictureBox. The user may want to
move or delete the file that is being shown. The corresponding event-
driven code for "delete this picture" uses:

My.Computer.FileSystem.DeleteFile(sPath)

The exception which is thrown tells me that the system cannot access
the file as it's "being used by another process". What other process?
(There is no multithreading in the app.) Presumably this means that
you can't do anything with a jpg image file while it's being displayed
in a PictureBox (whereas you could with VB6/W98). Annoying, but I
decided on a workaround that meant blanking out the PictureBox before
attempting to delete the file. I've experimented with several methods,
including loading a new image from a different (dummy) jpg file,
setting the Image property to a bitmap created on the fly, or just
doing this before the delete:

frmMainForm.picPreview.Image.Dispose()

Still no luck - or rather, sometimes the deletion takes place without
problems, but most of the time it throws an exception, and if you loop
around and try again and again for a minute or two, usually the
exceptions just keep on coming. Still more mysteriously, tests suggest
that the system finally frees up the image file for deletion after a
few seconds in some cases (counting from where the PictureBox is
loaded with another image), or after a few minutes in others.

Can anyone figure out what's going on?

(I know that in theory you could dispense with the PictureBox
completely and just display the image using GDI+ graphics methods on
the form surface, but there are various reasons why implementing such
a solution would be hugely time-consuming for this app.)

Jun 27 '08 #1
4 5528
Gerard,

What is hugely time-consuming with drawing an Image on the surface, I am
not aware of the fact that after the scene is another method used for
drawing on the picturebox background surface.

Beside that does this simple code work for me.

PictureBox1.Load("d:\test.jpg")
IO.File.Delete("d:\test.jpg")

Cor
<ge************@yahoo.comschreef in bericht
news:93**********************************@m36g2000 hse.googlegroups.com...
Any help appreciated.

(VB.NET under XP and Vista, SP1 installed)

My code, inherited from a VB6 version of an app that ran under W98,
loads an image from a file into a PictureBox. The user may want to
move or delete the file that is being shown. The corresponding event-
driven code for "delete this picture" uses:

My.Computer.FileSystem.DeleteFile(sPath)

The exception which is thrown tells me that the system cannot access
the file as it's "being used by another process". What other process?
(There is no multithreading in the app.) Presumably this means that
you can't do anything with a jpg image file while it's being displayed
in a PictureBox (whereas you could with VB6/W98). Annoying, but I
decided on a workaround that meant blanking out the PictureBox before
attempting to delete the file. I've experimented with several methods,
including loading a new image from a different (dummy) jpg file,
setting the Image property to a bitmap created on the fly, or just
doing this before the delete:

frmMainForm.picPreview.Image.Dispose()

Still no luck - or rather, sometimes the deletion takes place without
problems, but most of the time it throws an exception, and if you loop
around and try again and again for a minute or two, usually the
exceptions just keep on coming. Still more mysteriously, tests suggest
that the system finally frees up the image file for deletion after a
few seconds in some cases (counting from where the PictureBox is
loaded with another image), or after a few minutes in others.

Can anyone figure out what's going on?

(I know that in theory you could dispense with the PictureBox
completely and just display the image using GDI+ graphics methods on
the form surface, but there are various reasons why implementing such
a solution would be hugely time-consuming for this app.)
Jun 27 '08 #2
On Apr 20, 11:46*pm, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nl>
wrote:
Gerard,

What is *hugely time-consuming with drawing an Image on the surface, I am
not aware of the fact that after the scene is another method used for
drawing on the picturebox background surface.

Beside that does this simple code work for me.

PictureBox1.Load("d:\test.jpg")
IO.File.Delete("d:\test.jpg")
Something similar worked for me, too, until I tested in XP/Vista,
where the trouble started. Oddly, the exception is only thrown
sometimes, as I said.

I searched this newsgroup and eventually came across someone who had
the same problem. He was told to clone the image and then discard it.
Currently I'm testing the following (a variant of what was suggested):
Public Sub LoadImageClone(ByVal sPath As String, ByVal picPicBox
As PictureBox)
Dim bmpClone As Bitmap 'the clone to be loaded to a PictureBox

Dim bmpOriginal As New Bitmap(sPath) 'original file's bitmap,
original size

bmpClone = New Bitmap(bmpOriginal.Width, bmpOriginal.Height)
'create clone, initially empty, same size

Dim gr As Graphics = Graphics.FromImage(bmpClone) 'get object
representing clone's currently empty drawing surface
gr.DrawImage(bmpOriginal, 0, 0) 'copy original image onto this
surface
gr.Dispose()

bmpOriginal.Dispose()

picPicBox.Image = bmpClone 'assign the clone to picture box

End Sub

It seems to work around 90% of the time, but there are occasional
sizing problems. I'll post the fix if I can figure it out. Thanks for
replying.

>
Cor

<gerardianle...@yahoo.comschreef in berichtnews:93**********************************@m 36g2000hse.googlegroups.com...
Any help appreciated.
(VB.NET under XP and Vista, SP1 installed)
My code, inherited from a VB6 version of an app that ran under W98,
loads an image from a file into a PictureBox. The user may want to
move or delete the file that is being shown. The corresponding event-
driven code for "delete this picture" uses:
* * * *My.Computer.FileSystem.DeleteFile(sPath)
The exception which is thrown tells me that the system cannot access
the file as it's "being used by another process". What other process?
(There is no multithreading in the app.) Presumably this means that
you can't do anything with a jpg image file while it's being displayed
in a PictureBox (whereas you could with VB6/W98). Annoying, but I
decided on a workaround that meant blanking out the PictureBox before
attempting to delete the file. I've experimented with several methods,
including loading a new image from a different (dummy) jpg file,
setting the Image property to a bitmap created on the fly, or just
doing this before the delete:
* * * *frmMainForm.picPreview.Image.Dispose()
Still no luck - or rather, sometimes the deletion takes place without
problems, but most of the time it throws an exception, and if you loop
around and try again and again for a minute or two, usually the
exceptions just keep on coming. Still more mysteriously, tests suggest
that the system finally frees up the image file for deletion after a
few seconds in some cases (counting from where the PictureBox is
loaded with another image), or after a few minutes in others.
Can anyone figure out what's going on?
(I know that in theory you could dispense with the PictureBox
completely and just display the image using GDI+ graphics methods on
the form surface, but there are various reasons why implementing such
a solution would be hugely time-consuming for this app.)- Hide quoted text -

- Show quoted text -
Jun 27 '08 #3
For the sake of anyone who might be looking for an answer, here's the
latest fix. The routine below is used to load a clone of the original
image into the picture box. Once this is done, the original image file
can be moved, deleted, or whatever. (I have a variant of the following
for thumbnail images, using ScaleTransform, which seems to work fine
too).
Public Sub LoadImageClone(ByVal sPath As String, ByVal picPicBox
As PictureBox)
Dim bmpClone As Bitmap 'the clone to be loaded to a PictureBox
Dim bmpOriginal As System.Drawing.Image =
System.Drawing.Image.FromFile(sPath) 'original file's bitmap, original
size

bmpClone = New Bitmap(bmpOriginal.Width, bmpOriginal.Height)
'create clone, initially empty, same size

Dim gr As Graphics = Graphics.FromImage(bmpClone) 'get object
representing clone's currently empty drawing surface
gr.SmoothingMode = Drawing2D.SmoothingMode.None
gr.InterpolationMode =
Drawing2D.InterpolationMode.NearestNeighbor
gr.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighSpeed
gr.DrawImage(bmpOriginal, 0, 0, bmpOriginal.Width,
bmpOriginal.Height) 'copy original image onto this surface
gr.Dispose()

bmpOriginal.Dispose()

picPicBox.Image = bmpClone 'assign the clone to picture box

End Sub
Jun 27 '08 #4
Thanks for sharing, works for me.

<ge************@yahoo.comwrote in message
news:4b**********************************@i36g2000 prf.googlegroups.com...
For the sake of anyone who might be looking for an answer, here's the
latest fix. The routine below is used to load a clone of the original
image into the picture box. Once this is done, the original image file
can be moved, deleted, or whatever. (I have a variant of the following
for thumbnail images, using ScaleTransform, which seems to work fine
too).
Public Sub LoadImageClone(ByVal sPath As String, ByVal picPicBox
As PictureBox)
Dim bmpClone As Bitmap 'the clone to be loaded to a PictureBox
Dim bmpOriginal As System.Drawing.Image =
System.Drawing.Image.FromFile(sPath) 'original file's bitmap, original
size

bmpClone = New Bitmap(bmpOriginal.Width, bmpOriginal.Height)
'create clone, initially empty, same size

Dim gr As Graphics = Graphics.FromImage(bmpClone) 'get object
representing clone's currently empty drawing surface
gr.SmoothingMode = Drawing2D.SmoothingMode.None
gr.InterpolationMode =
Drawing2D.InterpolationMode.NearestNeighbor
gr.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighSpeed
gr.DrawImage(bmpOriginal, 0, 0, bmpOriginal.Width,
bmpOriginal.Height) 'copy original image onto this surface
gr.Dispose()

bmpOriginal.Dispose()

picPicBox.Image = bmpClone 'assign the clone to picture box

End Sub

Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
4
by: Saso Zagoranski | last post by:
Hi! Here is what I'm trying to do: I have created a UserControl named PictureView. It holds all the images in one directory in the Bitmap images variable. The selected image is displayed in...
1
by: Ing. Rajesh Kumar | last post by:
I am loading an XML File and validating that against an XSD file. Sometimes when there is an error in the XML file, system locks the file and I am not able to change the content of the file. When...
4
by: lgbjr | last post by:
Hello All, I have a picturebox on a VB.NET form. I use an OpenfileDialog to select an image for the picturebox (pb.image=image.fromfile(Openfiledialog.fileame)). Once the image is loaded, I want...
6
by: Jos Roijakkers | last post by:
At some point in my application I load an image into a picturebox with the code: pbGraphic.Image = Image.FromFile("picture.jpg") Later on in the program I want to rename the file using the...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
11
by: fniles | last post by:
One of our application uses VB6 and Access97 database. Another application uses VB.NET 2005. This morning for about 15 seconds when the application tries to read either a query or a table from the...
3
by: flea2k | last post by:
I am trying to run this select statment against my DB2 database and it is throwing the following error, is there a setting in my db2 database to allow for this call "USE AND KEEP UPDATE LOCKS"? ...
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: 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?
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
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,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.