Results 1 to 3 of 3

Thread: How to create Animated GIFs

Hybrid View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2016
    Posts
    100

    How to create Animated GIFs

    Hey everyone, I just wanted to say that VBforums is awesome! Out of every online community I have been in, this one has been the most supportive. Thanks!

    Anyway, I have been trying for a couple of days now at work to compile a series of images into an animated GIF using the GDI+ library. I don't have access to any other image processing libraries so if at all possible I'd like to stick to using that one. I am putting together a program to automate some on screen actions in a 3D program and take screen shots at the same time. Those screen shots are saved to the Hard drive and are named chronologically. So far this functionality has been developed and is working well.

    At this point though I am not sure what to do. I have seen some online examples in C# using the GDI+ library but I don't understand it. I have been studying and testing portions of code from the following link:

    http://www.dreamincode.net/forums/to...ssing-program/

    Specifically the Assemble() subroutine where according to the author is where the animated GIF is made (Below it can be viewed).

    There is miscellaneous code in there pertaining to Form controls, but besides those, it seems like the images are being looped through one at a time and being opened with BinaryWriter, and eventually saved to a MemoryStream in the format of a GIF. At the end of the subroutine it looks like the compiled GIF is displayed in a PictureBox form control. At least this is what I gathered so far.

    I don't quite understand how the image is being assembled though. I have never used MemoryStream or BinaryWriter before and it looks the image is saved to MemoryStream before it's finally displayed. Also, what are the Bytes data type for and what does that accomplish?

    Can somebody help me make sense of what is going on this code. Is this going to help me create and save an animated GIF with my screenshots or should I resort to using something else?

    Thank you for your time.

    Code:
        Public Sub Assemble(ByVal But As Integer)
            Me.Cursor = Cursors.WaitCursor
            'Variable declaration
            Dim memoryStream As MemoryStream
            Dim binaryWriter As BinaryWriter
            Dim image As Image
            Dim buf1 As [Byte]()
            Dim buf2 As [Byte]()
            Dim buf3 As [Byte]()
            memoryStream = New MemoryStream()
            buf2 = New [Byte](18) {}
            buf3 = New [Byte](7) {}
            buf2(0) = 33
            'extension introducer
            buf2(1) = 255
            'application extension
            buf2(2) = 11
            'size of block
            buf2(3) = 78
            'N
            buf2(4) = 69
            'E
            buf2(5) = 84
            'T
            buf2(6) = 83
            'S
            buf2(7) = 67
            'C
            buf2(8) = 65
            'A
            buf2(9) = 80
            'P
            buf2(10) = 69
            'E
            buf2(11) = 50
            '2
            buf2(12) = 46
            '.
            buf2(13) = 48
            '0
            buf2(14) = 3
            'Size of block
            buf2(15) = 1
            '
            buf2(16) = 0
            '
            buf2(17) = 0
            '
            buf2(18) = 0
            'Block terminator
            buf3(0) = 33
            'Extension introducer
            buf3(1) = 249
            'Graphic control extension
            buf3(2) = 4
            'Size of block
            buf3(3) = 9
            'Flags: reserved, disposal method, user input, transparent color
            buf3(4) = 10 ' 88 
            If ckSlide.Checked = True Then
                'Delay time low byte
                buf3(5) = 1
                'Delay time high byte
                buf3(6) = 0
            Else
                'Delay time low byte
                buf3(5) = 0 '1 
                'Delay time high byte
                buf3(6) = 255
            End If
    
            'Transparent color index
            buf3(7) = 0
            'Block terminator
    
            NewName = GetNewName1()
            lbOutput.Text = NewName
            binaryWriter = New BinaryWriter(File.Open(NewName, FileMode.Create))
    
            For picCount As Integer = 0 To ImageArray.Count - 1 'stringCollection.Count - 1
    
                Select Case But
                    Case 1
                        'Animate
                        image = Bitmap.FromFile(ImageArray(picCount))
                        If ckDark.Checked = True Then image = Dark(image)
                        If rbSepiaTone.Checked = True Then image = Sepia(image)
                        If rbGrayScale.Checked = True Then image = Grayscale(image)
                        If ckGhost.Checked = True Then Ghost(image)
                        If ckFlip.Checked = True Then image.RotateFlip(RotateFlipType.RotateNoneFlipX)
                    Case 2
                        If MemPic IsNot Nothing Then
                            image = MemPic(picCount)
                            If ckDark.Checked = True Then image = Dark(image)
                            If rbSepiaTone.Checked = True Then image = Sepia(image)
                            If rbGrayScale.Checked = True Then image = Grayscale(image)
                            If ckGhost.Checked = True Then Ghost(image)
                            If ckFlip.Checked = True Then image.RotateFlip(RotateFlipType.RotateNoneFlipX)
                        Else
                            MessageBox.Show("Please Select Something")
                        End If
    
                End Select
                image.Save(memoryStream, ImageFormat.Gif)
                buf1 = memoryStream.ToArray()
    
                If picCount = 0 Then
                    'only write these the first time....
                    binaryWriter.Write(buf1, 0, 781)
                    'Header & global color table
                    'Application extension
                    binaryWriter.Write(buf2, 0, 19)
                End If
                binaryWriter.Write(buf3, 0, 8)
                'Graphic extension
                binaryWriter.Write(buf1, 781, buf1.Length - 782)
                'Image data
                ' If picCount = stringCollection.Count - 1 Then
                If picCount = ImageArray.Count - 1 Then
                    'only write this one the last time....
                    'Image terminator
                    binaryWriter.Write(";")
                End If
    
    
                memoryStream.SetLength(0)
                Progress.Value = 100 * picCount / ImageArray.Count
            Next
    
            binaryWriter.Close()
            pbSelect.Invalidate()
            memoryStream.Close()
            Me.Cursor = Cursors.Default
            pbSelect.Image = image.FromFile(ImageArray(N))
            pbSelect.Refresh()
            Progress.Value = 0
        End Sub

  2. #2
    Hyperactive Member
    Join Date
    Oct 2016
    Posts
    369

    Re: How to create Animated GIFs

    I think joaquim has animated gif code examples in the Code Bank

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to create Animated GIFs

    Though original post is about 3 months old, following provided should others find this thread...

    The GIF format is well documented and available online. Search for: GIF specification

    Those bytes in your sample project define GIF blocks, per its specifications. GDI+, version 1.0, cannot create animated GIFs directly. This may be what the code's author is dealing with: create each frame and append it to a binary stream/file. Version 1.1, which can be used in VB6 with a manifest, can produce animated GIFs. However, it is very limited in efficiency and typically produces larger GIF files than a good GIF creation application would produce.

    If GDI+ is used: Advantage is that you don't really need to know GIF structure in-depth. Disadvantage: larger GIF files, requires knowing how to create suitable 8-bit images for each frame

    If pure VB is used without GDI+. Advantage is more customization per frame and producing smaller GIF files. Disadvantage is that being completely knowledgeable of the GIF structure is required and you need to deal with compressing/encoding the data on your own.

    Even my GDI+ based AlphaImage control can create animated GIFs using GDI+ v1.0. Not suggesting you use it, but there are plenty of comments in the included GIF class that describes how we can get it to work, even though that GDI+ version doesn't support creation of animated GIFs. The process appends frames, with some optimization, similar to your posted example.

    Edited: The sample code you linked to uses VB.Net but my Alpha Image Control (linked in my signature below) written in VB6. The logic should be transferable, but not the code.

    Edited yet again. I'd be surprised that most recent versions of .Net cannot use the latest version of GDI+. If it can, then there must be a better example of .Net creating an animated GIF without resorting to manual frame by frame appends.
    Last edited by LaVolpe; Mar 18th, 2017 at 07:41 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width