943,627 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 1365
  • VB.NET RSS
Jun 16th, 2009
0

[saving animated GIF] Help...

Expand Post »
if I want to send an animated GIF format picture, how do I save the picture in destination folder?
I already using "System.Drawing.Imaging.ImageFormat.gif", but the GIF picture no longer animated, it become like JPEG format.
another help please...
Similar Threads
Reputation Points: 21
Solved Threads: 1
Light Poster
murid is offline Offline
48 posts
since May 2009
Jun 16th, 2009
0

Re: [saving animated GIF] Help...

Could you please post your current image saving code. Thanks!
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jun 16th, 2009
1

Re: [saving animated GIF] Help...

Never mind the code. Here's a (generic) function to save byte array to a file (VB.NET 2005 and newer)
VB.NET Syntax (Toggle Plain Text)
  1. Public Function SaveFile(ByVal FileName As String, ByRef Buffer() As Byte, ByVal OverWrite As Boolean) As Boolean
  2. ' Save a bytearray to a file
  3. ' Returns True if file is saved, otherwise returns false
  4. ' Notice: Exception handling is missing!
  5. If My.Computer.FileSystem.FileExists(FileName) Then
  6. If OverWrite Then
  7. My.Computer.FileSystem.DeleteFile(FileName)
  8. My.Computer.FileSystem.WriteAllBytes(FileName, Buffer, False)
  9. Return True
  10. Else
  11. ' Don't save, file exists
  12. Return False
  13. End If
  14. Else
  15. My.Computer.FileSystem.WriteAllBytes(FileName, Buffer, False)
  16. Return True
  17. End If
  18.  
  19. End Function
And you call it (example):
VB.NET Syntax (Toggle Plain Text)
  1. If SaveFile("C:\my.gif", ByteBuffer, False) Then
  2. MessageBox.Show("File saved successfully", "Save File", MessageBoxButtons.OK, MessageBoxIcon.Information)
  3. Else
  4. MessageBox.Show("File exists or could not be written", "Save File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  5. 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.
Last edited by Teme64; Jun 16th, 2009 at 6:06 am. Reason: Added comment about animated GIFs
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jun 17th, 2009
0

Re: [saving animated GIF] Help...

thanks for the code...
I'll try it first...
Reputation Points: 21
Solved Threads: 1
Light Poster
murid is offline Offline
48 posts
since May 2009
Jun 17th, 2009
0

Re: [saving animated GIF] Help...

VB.NET Syntax (Toggle Plain Text)
  1. Dim PACKET_SIZE As UInt16 = 4096
  2. Dim Reader As BinaryReader
  3. Dim ReadBuffer(PACKET_SIZE - 1) As Byte
  4. Dim NData As Int32
  5. Dim MStream As MemoryStream
  6. Dim LData As Int32
  7.  
  8. Reader = New BinaryReader(clientSocket.GetStream)
  9. ' Read Length of data (Int32)
  10. NData = Reader.ReadInt32
  11. ' Now comes the data, save it in a memory stream
  12. MStream = New MemoryStream
  13. While NData > 0
  14. LData = clientSocket.GetStream.Read(ReadBuffer, 0, PACKET_SIZE)
  15. MStream.Write(ReadBuffer, 0, LData)
  16. NData -= LData
  17. End While
  18.  
  19. If SaveFile("C:\my.gif", ReadBuffer, False) Then
  20. MessageBox.Show("File saved successfully", "Save File", MessageBoxButtons.OK, MessageBoxIcon.Information)
  21. Else
  22. MessageBox.Show("File exists or could not be written", "Save File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  23. 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...
Reputation Points: 21
Solved Threads: 1
Light Poster
murid is offline Offline
48 posts
since May 2009
Jun 17th, 2009
0

Re: [saving animated GIF] Help...

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
VB.NET Syntax (Toggle Plain Text)
  1. If SaveFile("C:\my.gif", MStream.ToArray(), False) Then
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
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jul 3rd, 2009
0

Re: [saving animated GIF] Help...

Your code working GREAT !!
Finally I found what's wrong...
my mistake is I save the selected GIF picture to JPG format before displaying it...
by doing that, I send and save the JPG format instead of the GIF file...
that's why my GIF never move...

sorry to trouble U.
Reputation Points: 21
Solved Threads: 1
Light Poster
murid is offline Offline
48 posts
since May 2009
Jul 3rd, 2009
0

Re: [saving animated GIF] Help...

Quote ...
Your code working GREAT !!
Great!

Quote ...
sorry to trouble U
There wasn't any trouble
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Crystal Reports - Generating Spreadsheet
Next Thread in VB.NET Forum Timeline: Server dir list in network





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC