File Created Not Accessible Until App Shuts Down

G

Guest

I am using the filestream feature in VB.NET to create a file. However, I am not able to delete or overwrite the file until the app closes down. I am using the .Close( ) method on the filestream right after it is created. I am creating a .BMP file from an OLE Object of an Access database field. After creating the .BMP file, I read it back into a System.Drawing.Image object (in order to display the bitmap on my form), using the " FromFile('c:\sign.bmp')" method (and have also used " = New Bitmap('c:\sign.bmp')" as well). It is this command that puts some kind of lock on the file - with that remarked out, I can do anything with the file I want to. Here is a code snippet
----------------------------------------------------------------
Dim imgSignature As System.Drawing.Imag
Dim imgData() As Byt

imgData = dr.Item("StudentSign"
Dim fs As New FileStream("c:\sign.bmp", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite
fs.Write(imgData, 0, UBound(imgData)
fs.Close(
fs = Nothin
picSignature.Image = imgSignature.FromFile("c:\sign.bmp"
imgSignature = Nothin
----------------------------------------------------------------

Once that " = imgSignature.FromFile('c:\sign.bmp')" code is executed, the sign.bmp file is inaccessible (cannot be deleted either programatically or through Windows Explorer). I tried to use "imgSignature.Dispose()", and that caused argument errors just trying to start the application

Any ideas

- Thanks

JR
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?Sm9obiBEb3VnaGVydHk=?= said:
I am using the filestream feature in VB.NET to create a file. However, I am not able to delete or overwrite the file until the app closes down. I am using the .Close( ) method on the filestream right after it is created. I am creating a .BMP file from an OLE Object of an Access database field. After creating the .BMP file, I read it back into a System.Drawing.Image object (in order to display the bitmap on my form), using the " FromFile('c:\sign.bmp')" method (and have also used " = New Bitmap('c:\sign.bmp')" as well). It is this command that puts some kind of lock on the file - with that remarked out, I can do anything with the file I want to. Here is a code snippet:
-----------------------------------------------------------------
Dim imgSignature As System.Drawing.Image
Dim imgData() As Byte

imgData = dr.Item("StudentSign")
Dim fs As New FileStream("c:\sign.bmp", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)
fs.Write(imgData, 0, UBound(imgData))
fs.Close()
fs = Nothing
picSignature.Image = imgSignature.FromFile("c:\sign.bmp")
imgSignature = Nothing

Does 'imgSignature.Dispose()' help?
 
C

Cor Ligthert

Once that " = imgSignature.FromFile('c:\sign.bmp')" code is executed,
the sign.bmp file is inaccessible (cannot be deleted either programatically
or through Windows Explorer). I tried to use "imgSignature.Dispose()", and
that caused argument errors just trying to start the application.
Does 'imgSignature.Dispose()' help?

Are you sure of this answer?

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
the sign.bmp file is inaccessible (cannot be deleted either programatically
or through Windows Explorer). I tried to use "imgSignature.Dispose()", and
that caused argument errors just trying to start the application.

Are you sure of this answer?

Yes, but it won't really solve the OP's problem.
 
C

Chris Dunaway

I am using the filestream feature in VB.NET to create a file. However, I am not able to delete or overwrite the file until the app closes down. I am using the .Close( ) method on the filestream right after it is created. I am creating a .BMP file from an OLE Object of an Access database field. After creating the .BMP file, I read it back into a System.Drawing.Image object (in order to display the bitmap on my form), using the " FromFile('c:\sign.bmp')" method (and have also used " = New Bitmap('c:\sign.bmp')" as well). It is this command that puts some kind of lock on the file - with that remarked out, I can do anything with the file I want to. Here is a code snippet:

Try loading the .bmp using a FileStream and then use the image object's
FromStream method.
 
C

Cor Ligthert

Hi Chris,

I sended this already to someone else today, did you see this sample from me
before.
The XML file is representing the database however is not important what
databas, when it is for a Ole embeded image from a non 97 access database
it needs a little extention, that I have also.

Cor

\\\
Private abyt() As Byte
Private fo As New OpenFileDialog
Private sf As New SaveFileDialog
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Reading a picture and put it in a bytearray
If fo.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(fo.FileName, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
abyt = br.ReadBytes(CInt(fs.Length))
br.Close()
'just to show the sample without a fileread error
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button2.Click
'writing a picture from a bytearray
If sf.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(sf.FileName, _
IO.FileMode.CreateNew)
Dim bw As New IO.BinaryWriter(fs)
bw.Write(abyt)
bw.Close()
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button3.Click
'writing a bytearray to a dataset
Dim ds As New DataSet
ds.Tables.Add(New DataTable("Photo"))
ds.Tables(0).Columns.Add(New DataColumn("Sample"))
ds.Tables(0).Columns(0).DataType =
System.Type.GetType("System.Byte[]")
ds.Tables(0).Rows.Add(ds.Tables(0).NewRow)
ds.Tables(0).Rows(0)(0) = abyt
Dim sf As New SaveFileDialog
If sf.ShowDialog = DialogResult.OK Then
ds.WriteXml(sf.FileName, XmlWriteMode.WriteSchema)
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button4.Click
'reading a picture from a dataset
Dim ds As New DataSet
If fo.ShowDialog = DialogResult.OK Then
ds.ReadXml(fo.FileName)
End If
abyt = CType(ds.Tables(0).Rows(0)(0), Byte())
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End Sub
///
 
G

Guest

Thanks for the help, Chris! Your idea took care of the problem. I changed to use a 2nd filestream to read the .BMP image
back into my image control, and now the file can be deleted

Dim fs2 As New FileStream("c:\sign.bmp", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite
Dim imgData2(fs2.Length() - 1) As Byt
fs2.Read(imgData2, 0, fs2.Length
picSignature.Image = imgSignature.FromStream(fs2
fs2.Close(
fs2 = Nothin

So apparently, using the filestream, rather than the FromFile method, doesn't put some kind of lock on the file (or whatever it was doing!)

- JRD
 

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