Unlocking file when assigned to PictureBox

M

Mark

Hello,

Say I write a picture file to: 'c:\test.jpg'

Then I assign this picture to a picturebox
PbFoto.Image = Image.Fromfile("c:\test.jpg")

Next I want the overrwrite the file test.jpg with another picture.

When running, he says the file is in use when I want to overwrite it.
(if not assignd to the picture box, I can overwritte as many as I want)

I've tried 'PbFoto.Image = Nothing', but that didn't help also.

Anyone an idea how to free the file?
 
C

Chris Dunaway

Hello,

Say I write a picture file to: 'c:\test.jpg'

Then I assign this picture to a picturebox
PbFoto.Image = Image.Fromfile("c:\test.jpg")

Next I want the overrwrite the file test.jpg with another picture.

When running, he says the file is in use when I want to overwrite it.
(if not assignd to the picture box, I can overwritte as many as I want)

I've tried 'PbFoto.Image = Nothing', but that didn't help also.

Anyone an idea how to free the file?

Look elsewhere in these groups for more information, but I believe the
solution offered is to use the FromStream method instead of using the
Image.FromFile method. When using FromStream, the file is not locked.

Perhaps this code can help:

Imports System.IO
Imports System.Drawing

Dim fs As System.IO.FileStream
fs = New FileStream("C:\test.jpg", FileMode.Open, FileAccess.Read)
PictureBox1.Image = Image.FromStream(fs)
fs.Close()


--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
A

Atara

for memory optimization reasons GDI+ locks the file.

' --------------------------------------
' 1. Final Version:
'! myP.Image = Image.FromFile(src)
' --------------------------------------
' 2. Edit Version:
' based on MSDN:
' Image File Is Locked When You Set the PictureBox Image
' Property to a File
' http://support.microsoft.com/default.aspx?scid=kb;;Q309482
Dim fs As System.IO.FileStream
fs = New System.IO.FileStream(src, IO.FileMode.Open, IO.FileAccess.Read)
'TODO: Add exeption: 'Directory not found'
'TODO: Add exeption: 'System.IO.FileNotFoundException'
myP.Image = System.Drawing.Image.FromStream(fs)
fs.Close()
' --------------------------------------
 

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