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...

Recommended Answers

All 7 Replies

Could you please post your current image saving code. Thanks!

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.

thanks for the code...
I'll try it first...

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

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 :-/

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.

Your code working GREAT !!

Great!

sorry to trouble U

There wasn't any trouble :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.