PictureBox "locks" file, cannot move/delete

G

gerardianlewis

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.)
 
C

Cor Ligthert[MVP]

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
 
G

gerardianlewis

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.
 
G

gerardianlewis

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
 
B

BidyaSagar Jena

Hi
You can display the image in the picturebox like this
Stream s = File.Open(filePath);
Image temp = Image.FromStream(s);
s.Close();
pictureBox1.Image = temp;

After that u can easly delete the tempimage file.

Thnk you
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top