When I get the image from the file the file remains locked

D

**Developer**

When I get the image from the file the file remains locked so the Delete
fails with a "used by another process"

So I tried using a clone and disposing the obtained image.

But that didn't fix the problem.

Can you help?



Dim zz As Image = Image.FromFile(imageFileName)

MyImage = zz.Clone

zz.Dispose()

zz = Nothing

File.Delete(imageFileName)
 
L

Lucky

hi,
the problem here with cloning method is it also copies the reference
of the file that is loaded in the IMAGE object so that if you dispose
the zz object after cloning, it passes reference of the file to MyImage
object that's why ultimately the file remains locked.

try this code. this should solve your problem

Dim fs As New StreamReader("C:\all.jpg")
Dim img As Image = Image.FromStream(fs.BaseStream)
fs.Close()
PictureBox1.Image = img
File.Delete("C:\all.jpg")


Lucky
 
M

Michael D. Ober

I'd really like to know if this works. The Kodak Image drivers in Windows
98 and NT4 have the same problem. I always thought this was a bug in the
Kodak drivers, but if it exists in .NET (which doesn't use the Kodak
drivers) as well, then the problem may actually be in the underlying Win32
API.

Mike Ober.
 
L

Lucky

well first you need to understand the behaviour of the class and the
way it treats the resources.

in your case IMAGE class lock the resources so that if any call for
refresh/reload needs to attend by the object than the original data can
be reloaded or modified by the object itself.

i dont know why it is like this in IMAGE class but MS has implimented
it this way only.

run my code and compare it with your's and you'll see the difference
and behaviour of the class

Lucky
 
D

**Developer**

I don't think I can use your suggestion. I try to make it easy to see the
problem by shorting the code but I'm afriad I cut out too much code. Below
might be more then required to show the problem but I hope I didn't cut
anything relavent.



I believe the problen code starts where I left the blank lines below.



Dim wiaManager As WiaClass = Nothing ' WIA manager COM object

Dim wiaDevs As CollectionClass = Nothing ' WIA devices collection COM object

Dim wiaRoot As ItemClass = Nothing ' WIA root device COM object

Dim wiaPics As CollectionClass = Nothing ' WIA collection COM object

Dim wiaItem As ItemClass = Nothing ' WIA image COM object

Dim imageFileName As String

wiaManager = New WiaClass ' create COM instance of WIA manager

wiaDevs = wiaManager.Devices '

wiaDevs = wiaManager.Devices ' as CollectionClass; ' call Wia.Devices to get
all devices

If wiaDevs Is Nothing OrElse wiaDevs.Count = 0 Then

MessageBox.Show("No WIA devices found!", "WIA", MessageBoxButtons.OK,
MessageBoxIcon.Stop)

Application.Exit()

Return Nothing

End If

Dim selectUsingUI As Object = System.Reflection.Missing.Value ' = Nothing

wiaRoot = CType(wiaManager.Create(selectUsingUI), ItemClass) ' Display form
to let the user select device

If wiaRoot Is Nothing Then ' nothing to do

Return Nothing

End If

wiaPics = wiaRoot.GetItemsFromUI(WiaFlag.SingleImage,
WiaIntent.ImageTypeColor) 'Open acquisition form to get a single image.

If wiaPics Is Nothing Then

Return Nothing

End If





Dim wiaObj As Object = wiaPics.Item(0)

wiaItem = CType(Marshal.CreateWrapperOfType(wiaObj, GetType(ItemClass)),
ItemClass)

imageFileName = Path.GetTempFileName() ' create temporary file for image

wiaItem.Transfer(imageFileName, False) ' Now do the scan and then transfer
picture to our temporary file (only way to get it)

Dim zz As Image = Image.FromFile(imageFileName) ' create Image instance from
file

AcquireScanner = zz.Clone

zz.Dispose() 'Unlock the file

zz = Nothing

File.Delete(imageFileName)


When I get the image from the file the file remains locked so the Delete
fails with a "used by another process"
So I tried using a clone and disposing the obtained image.
 
D

**Developer**

What I mean
is the file there forever or does Window eventually delete it




**Developer** said:
What is the penalty for not deleting a temp file?

Is that a way to go?
 
L

Lucky

i'm not sure when and how windows manages file in temp folder but
deleting files from code after use, is best practice.

Lucky
 
M

Michael D. Ober

I still think it's a bug in the GDI itself. When you close the references
to an image using the COM interface, the image control should go away. It
doesn't - I know because I tested this heavily and finally discovered that
the only way to reliably "unlock" the file is to set the image to another
file first.

Mike.
 
M

Michael D. Ober

Based on my experience with the Kodak drivers, you get an error when you
attempt to delete the file. Windows won't delete it later.

Mike Ober.

**Developer** said:
What I mean
is the file there forever or does Window eventually delete it
 
D

**Developer**

Thanks for the two replies.
Did you see Lucky's second post?
Sound reasonable to me.
How does it sound to you?
 
M

Michael D. Ober

I solved my problem (7 years ago) by keeping a "dummy.jpg" around that I
assign to the control. Once I started doing this, I was able to delete the
original file.

Mike.
 
D

**Developer**

Sounds like a good work around.
Based on Lucky's post I looked to see if there file reference was
available - but found nothing.

thanks
 
B

Barry

This works for me... after you close the lcl_fso the file can be
deleted, just don't get rid of the byte_old.

Dim lcl_filename As System.String = "c:\temp\images\test.tif"
Dim lcl_fso As New System.IO.FileStream(lcl_filename, _
IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
Dim byte_old() As Byte
Dim tmpint As Integer = Convert.ToInt32(lcl_fso.Length())
With lcl_fso
ReDim byte_old(tmpint)
.Read(byte_old, 0, tmpint)
End With
lcl_fso.Close()
Dim memstream As New System.IO.MemoryStream(byte_old)
Dim lcl_holdimage As System.Drawing.Image = _
System.Drawing.Image.FromStream(memstream)

HTH,
Barry

Sounds like a good work around.
Based on Lucky's post I looked to see if there file reference was
available - but found nothing.

thanks

bceggersATcomcastDOTnet
 
D

**Developer**

Works for me too.
I'll bet I not the only one that will copy your approach.

Thanks
 

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