| | |
[saving animated GIF] Help...
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
Never mind the code. Here's a (generic) function to save byte array to a file (VB.NET 2005 and newer) And you call it (example):
Another example:
One thing to be careful is array indexing which starts from the zero. If you declare an array
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.
VB.NET Syntax (Toggle Plain Text)
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
VB.NET Syntax (Toggle Plain Text)
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
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.
Last edited by Teme64; Jun 16th, 2009 at 6:06 am. Reason: Added comment about animated GIFs
Teme64 @ Windows Developer Blog
•
•
Join Date: May 2009
Posts: 41
Reputation:
Solved Threads: 1
VB.NET Syntax (Toggle Plain Text)
Dim PACKET_SIZE As UInt16 = 4096 Dim Reader As BinaryReader Dim ReadBuffer(PACKET_SIZE - 1) As Byte Dim NData As Int32 Dim MStream As MemoryStream Dim LData As Int32 Reader = New BinaryReader(clientSocket.GetStream) ' Read Length of data (Int32) NData = Reader.ReadInt32 ' Now comes the data, save it in a memory stream MStream = New MemoryStream While NData > 0 LData = clientSocket.GetStream.Read(ReadBuffer, 0, PACKET_SIZE) MStream.Write(ReadBuffer, 0, LData) NData -= LData End While If SaveFile("C:\my.gif", ReadBuffer, 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 If
it's my code went socket receive picture n save it.
still cannot save animated GIF picture.
the code are working perfectly, but the saved picture can't move...
I'm not changing anything for the function...
Actually you're saving incoming data to the memory stream, not in the temporary byte buffer. You should save the content of the memory stream.
Change this single line
ToArray method returns the content of the whole memory stream as a byte array.
And one reminder again. Close and dispose streams, sockets etc. after using them
If the code still doesn't work after making the code change above, could you please attach your original animated gif image. Not sure if I have in hand any easily findable animated gif to test with
Change this single line
VB.NET Syntax (Toggle Plain Text)
If SaveFile("C:\my.gif", MStream.ToArray(), False) Then
And one reminder again. Close and dispose streams, sockets etc. after using them

If the code still doesn't work after making the code change above, could you please attach your original animated gif image. Not sure if I have in hand any easily findable animated gif to test with
Teme64 @ Windows Developer Blog
•
•
•
•
Your code working GREAT !!
•
•
•
•
sorry to trouble U
Teme64 @ Windows Developer Blog
![]() |
Similar Threads
- animated pictures and .GIF files (Graphics and Multimedia)
- animated gif (DaniWeb Community Feedback)
- Animated GIF with Graphics, or Graphics2D? (Java)
- GD - Print Animated GIF (PHP)
- Animated gif in picturebox (C#)
- can i use animated gif? (Visual Basic 4 / 5 / 6)
- Animated GIF files (Python)
- Difficult animated gif problem. (Graphics and Multimedia)
Other Threads in the VB.NET Forum
- Previous Thread: Crystal Reports - Generating Spreadsheet
- Next Thread: Server dir list in network
| Thread Tools | Search this Thread |
"crystal .net .net2005 30minutes 2008 access add application arithmetic array assignment basic binary box button buttons center click code combo combobox component connectionstring convert cpu data database databasesearch datagrid datagridview design dissertation dissertations dissertationthesis dosconsolevb.net editvb.net employees excel exists firewall folder image images isnumericfuntioncall login math memory mobile module ms msaccess mssqlbackend mysql navigate net opacity pan peertopeervideostreaming picturebox picturebox1 port print printpreview problemwithinstallation project record regex reports" reuse right-to-left save savedialog search serial sorting sqldatbase storedprocedure string temp textbox timer txttoxmlconverter updown useraccounts usercontol usercontrol vb vb.net vb.netcode vb.nettoolboxvisualbasic2008sidebar vbnet view vista visual visualbasic visualbasic.net visualstudio web wpf xml





