Never mind the code. Here's a (generic) function to save byte array to a file (VB.NET 2005 and newer)
Public Function SaveFile(ByVal FileName As String, ByRef Buffer() As Byte, ByVal OverWrite As Boolean) As Boolean
' Save a bytearray to a file
' Returns True if file is saved, otherwise returns false
' Notice: Exception handling is missing!
If My.Computer.FileSystem.FileExists(FileName) Then
If OverWrite Then
My.Computer.FileSystem.DeleteFile(FileName)
My.Computer.FileSystem.WriteAllBytes(FileName, Buffer, False)
Return True
Else
' Don't save, file exists
Return False
End If
Else
My.Computer.FileSystem.WriteAllBytes(FileName, Buffer, False)
Return True
End If
End Function
And you call it (example):
If SaveFile("C:\my.gif", ByteBuffer, False) Then
MessageBox.Show("File saved successfully", "Save File", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("File exists or could not be written", "Save File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End
Another example:
SaveFile("C:\my.gif", ByteBuffer, True) saves the file and overwrites the file if it exists.
One thing to be careful is array indexing which starts from the zero. If you declare an array
Buffer(4) As Byte , you'll have an array with
five bytes (Buffer(0), Buffer(1),...Buffer(4)). Forgetting this can easily lead to "off-by-one" errors and hard-to-catch errors.
HTH
Use this code to save animated GIF. It might be possible that "normal" .NET's image saving methods (or file formats to be precise) do not support animated GIFs. Saving an animated GIF as "raw" data is 100% sure way to ensure it is saved correctly.