Data file into image

gpmaker

Member
Joined
Jun 3, 2004
Messages
24
OK, here's the problem. I'm trying to read a data file, and read it byte by byte and write it into a .gif file, here's my code:

VB.NET:
[size=2][color=#0000ff]Public [/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] DecodePicture([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] filename [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]String[/color][/size][size=2]) [/size][size=2][color=#0000ff]As[/color][/size][size=2] Bitmap
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] s1, s2 [/size][size=2][color=#0000ff]As[/color][/size][size=2] FileStream[/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] br [/size][size=2][color=#0000ff]As[/color][/size][size=2] BinaryReader[/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] bw [/size][size=2][color=#0000ff]As[/color][/size][size=2] BinaryWriter[/size]
 
[size=2]s1 = [/size][size=2][color=#0000ff]New[/color][/size][size=2] FileStream(filename, FileMode.Open, FileAccess.Read)[/size]
[size=2]s2 = [/size][size=2][color=#0000ff]New[/color][/size][size=2] FileStream("C:\Temp\temp.gif", FileMode.Create, FileAccess.Write)[/size]
[size=2]br = [/size][size=2][color=#0000ff]New[/color][/size][size=2] BinaryReader(s1)[/size]
[size=2]bw = [/size][size=2][color=#0000ff]New[/color][/size][size=2] BinaryWriter(s2)[/size]
 
[size=2][color=#0000ff]Dim[/color][/size][size=2] y [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] byteread [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Byte[/color][/size]
[size=2][color=#0000ff]For[/color][/size][size=2] y = 0 [/size][size=2][color=#0000ff]To[/color][/size][size=2] br.BaseStream.Length() - 1[/size]
[size=2]	 byteread = br.ReadByte
	 bw.Write(byteread)
[/size][size=2][color=#0000ff]Next[/color][/size]
 
[size=2]br.Close()
bw.Close()
s1.Close()
s2.Close()
br = [/size][size=2][color=#0000ff]Nothing[/color][/size]
[size=2]bw = [/size][size=2][color=#0000ff]Nothing[/color][/size]
[size=2]s1 = [/size][size=2][color=#0000ff]Nothing[/color][/size]
[size=2]s2 = [/size][size=2][color=#0000ff]Nothing[/color][/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] temp [/size][size=2][color=#0000ff]As[/color][/size][size=2] Bitmap = Image.FromFile("C:\Temp\temp.gif")[/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] decoded [/size][size=2][color=#0000ff]As[/color][/size][size=2] Bitmap = [/size][size=2][color=#0000ff]New[/color][/size][size=2] Bitmap(temp)[/size]
[size=2]temp = [/size][size=2][color=#0000ff]Nothing[/color][/size]
[size=2]Kill("C:\Temp\temp.gif")
[/size][size=2][color=#0000ff]Return[/color][/size][size=2] decoded[/size]
 
[size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Function
[/color][/size]

Now, this is a function that should work for any of my data files that i give it, but if i run the function more than once, it says "C:\Temp\temp.gif" is being used by another process or something. Also sometimes when I only use the function once, it stops at Kill("C:\Temp\temp.gif") and says the same thing. I don't get it, I'm closing everything.
 
Last edited:
In the code segment below,

Dim temp As Bitmap = Image.FromFile("C:\Temp\temp.gif")
Dim decoded As Bitmap = New Bitmap(temp)
temp = Nothing
Kill("C:\Temp\temp.gif")
Return decoded


the line
temp=Nothing
makes the object temp inaccessible, but does not dispose of it. As the object still exists even though not accessible, the Kill command is unable to act.

On the other hand, if you replace
temp=Nothing by
temp.dispose()
the object temp will be disposed of and will not cause problems with the Kill command. If you execute the modified procedure the second time, the file will no longer exist and will fail at the line
Dim temp As Bitmap = Image.FromFile("C:\Temp\temp.gif")
which is your expected result, as you stressed that you executed the function only once.

Happy Programming.
 
Back
Top