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

Process cannot access file while updating filestream used by picturebox

When I am updating a picturebox with an image (extracted) from an
access database I keep getting a "The process cannot access the file
"c:\foto1.jpg because it is being used by another process"
this only happens the second time I run it, the first time it works
out fine..

I've tried things like
MyPicBox.dispose
MyPicBox = nothing
etc,etc

All with no success.

Here's the code:

Public Sub GetPhoto(ByVal MyPhoto As String, ByVal MyPicBox As
PictureBox)
Try
Dim CurImage As Image = Nothing
Dim myRow As DataRow
myRow = myDSHout.Tables("ARTIKELINFO").Rows(0)

Dim MyData() As Byte
MyData = myRow(MyPhoto)
Dim K As Long
K = UBound(MyData)
Dim fs As New FileStream("c:\" & MyPhoto & ".JPG", _
FileMode.OpenOrCreate, FileAccess.Write,
FileShare.ReadWrite)

fs.Write(MyData, 0, K)
fs.Close()
' Display image
CurImage = Image.FromFile("c:\" & MyPhoto & ".JPG")
MyPicBox.Image = CurImage
MyPicBox.Invalidate()
'Object weer leegmaken
CurImage = Nothing
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Thanks in advance,

Mike
Nov 20 '05 #1
5 2406
maybe a fs=Nothing whould help?

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"Mike Dole" <m_******@hotmail.com> wrote in message
news:fd**************************@posting.google.c om...
When I am updating a picturebox with an image (extracted) from an
access database I keep getting a "The process cannot access the file
"c:\foto1.jpg because it is being used by another process"
this only happens the second time I run it, the first time it works
out fine..

I've tried things like
MyPicBox.dispose
MyPicBox = nothing
etc,etc

All with no success.

Here's the code:

Public Sub GetPhoto(ByVal MyPhoto As String, ByVal MyPicBox As
PictureBox)
Try
Dim CurImage As Image = Nothing
Dim myRow As DataRow
myRow = myDSHout.Tables("ARTIKELINFO").Rows(0)

Dim MyData() As Byte
MyData = myRow(MyPhoto)
Dim K As Long
K = UBound(MyData)
Dim fs As New FileStream("c:\" & MyPhoto & ".JPG", _
FileMode.OpenOrCreate, FileAccess.Write,
FileShare.ReadWrite)

fs.Write(MyData, 0, K)
fs.Close()
' Display image
CurImage = Image.FromFile("c:\" & MyPhoto & ".JPG")
MyPicBox.Image = CurImage
MyPicBox.Invalidate()
'Object weer leegmaken
CurImage = Nothing
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Thanks in advance,

Mike

Nov 20 '05 #2
"Mike Dole" <m_******@hotmail.com> schrieb

Untested:
' Display image
CurImage = Image.FromFile("c:\" & MyPhoto & ".JPG")
MyPicBox.Image = CurImage
Replace line above by:
MyPicBox.Image = new bitmap(CurImage)

Add:
CurImage.Dispose
MyPicBox.Invalidate()
'Object weer leegmaken
CurImage = Nothing


--
Armin

http://learn.to/quote
http://www.plig.net/nnq/nquote.html

Nov 20 '05 #3
Cor
Hi Armin,

Glad I saw this, I am busy with something like this and I forgot that
:-)
Cor
Nov 20 '05 #4
Gc.Collect!

I had a similar problem and I solved that one by using the Garbage
collector. See the code below where I woulod have putted it. It's up to you
to place it elsewhere! :)

It might slow down operations, but if you can live with that....

Good Luck and "Happy codeing" :)

//F

"Mike Dole" <m_******@hotmail.com> wrote in message
news:<fd**************************@posting.google. com>...
When I am updating a picturebox with an image (extracted) from an access database I keep getting a "The process cannot access the file "c:\foto1.jpg because it is being used by another process" this only happens the second time I run it, the first time it works out fine.. I've tried things like MyPicBox.dispose MyPicBox = nothing etc,etc All with no success. Here's the code: Public Sub GetPhoto(ByVal MyPhoto As String, ByVal MyPicBox As PictureBox) Try Dim CurImage As Image = Nothing Dim myRow As DataRow myRow = myDSHout.Tables("ARTIKELINFO").Rows(0) Dim MyData() As Byte MyData = myRow(MyPhoto) Dim K As Long K = UBound(MyData) Dim fs As New FileStream("c:\" & MyPhoto & ".JPG", _ FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite) fs.Write(MyData, 0, K) fs.Close() ' Display image CurImage = Image.FromFile("c:\" & MyPhoto & ".JPG") MyPicBox.Image = CurImage MyPicBox.Invalidate() 'Object weer leegmaken CurImage = Nothing
GC.COLLECT
Catch ex As Exception MsgBox(ex.ToString) End Try End Sub Thanks in advance, Mike

Nov 20 '05 #5
That did the trick Armin!

Thanks a million!

Mike

"Armin Zingler" <az*******@freenet.de> wrote in message news:<uY**************@TK2MSFTNGP12.phx.gbl>...
"Mike Dole" <m_******@hotmail.com> schrieb

Untested:
' Display image
CurImage = Image.FromFile("c:\" & MyPhoto & ".JPG")
MyPicBox.Image = CurImage


Replace line above by:
MyPicBox.Image = new bitmap(CurImage)

Add:
CurImage.Dispose
MyPicBox.Invalidate()
'Object weer leegmaken
CurImage = Nothing

Nov 20 '05 #6

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

Similar topics

3
by: trellow | last post by:
Hello, I am writing an application that needs to read a file that is already open by another process for writing. When I do the following: FileStream fs = new FileStream(fileName,...
6
by: lgbjr | last post by:
Hi All, As you know, I've made the switch from Access/Jet to MSDE 2K. And I'm now encountering my first little problem. I have some pictureboxes that display images from the DB. For each...
2
by: John | last post by:
I get this error while reading my config file but it is only occasionally and not all the time. I'm not sure why its happening. Does anyone know what could be going wrong? Thanks. The process...
0
by: imranabdulaziz | last post by:
Dear All, I am making web application using Asp.net C#(Visual Studio2005). And Sql server 2005 as a back End I generated local mode report but as there was no printing option available . I assign...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.