By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

Unlocking file when assigned to PictureBox

P: n/a
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?
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Wed, 22 Sep 2004 13:25:03 +0200, Mark wrote:
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.
Nov 21 '05 #2

P: n/a

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...id=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()
' --------------------------------------

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.