display single anigif frame and start/stop animation vb.net

I need to be able to select frames from an anigif and display them when and where I want to. That is, I want to display frame(0) in picturebox(0) and then frame(5) in picturebox(1), etc.

At the same time I want to run the complete anigif from frame (3) through (9) in a separate picturebox. I also need to stop it at frame 6 if I want to and then resume from frame, say, 8 later.

I need the vb.net (vb 2008) code.
chevronrodAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Perhaps this will get you started:
Imports System.Drawing.Imaging
Imports System.IO

Public Class Form1
    Private myGif As Image
    Private frameCount As Integer
    Private dimension As FrameDimension

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.myGif = New Bitmap("strike1.gif")
        Me.dimension = New FrameDimension(Me.myGif.FrameDimensionsList(0))
        Me.frameCount = Me.myGif.GetFrameCount(Me.dimension)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f1, f2, f3, f4 As New MemoryStream

        Me.myGif.SelectActiveFrame(Me.dimension, 3)
        Me.myGif.Save(f1, ImageFormat.Bmp)
        Me.PictureBox1.Image = Image.FromStream(f1)

        Me.myGif.SelectActiveFrame(Me.dimension, 1)
        Me.myGif.Save(f2, ImageFormat.Bmp)
        Me.PictureBox2.Image = Image.FromStream(f2)

        Me.myGif.SelectActiveFrame(Me.dimension, 5)
        Me.myGif.Save(f3, ImageFormat.Bmp)
        Me.PictureBox3.Image = Image.FromStream(f3)

        Me.myGif.SelectActiveFrame(Me.dimension, 2)
        Me.myGif.Save(f4, ImageFormat.Bmp)
        Me.PictureBox4.Image = Image.FromStream(f4)

        Me.PictureBox5.Image = Me.myGif
    End Sub
End Class

Open in new window

0
käµfm³d 👽Commented:
Oh, and as far as the starting/stopping, I believe you are going to have to keep track of which frame you want to stop on in a separate image object. Assign that image object to the "animated" picture box. When you are ready to restart, just assign the full gif back to the picture box and use SelectActiveFrame to start from the new index.

Like so:
Imports System.Drawing.Imaging
Imports System.IO

Public Class Form1
    Private myGif As Image
    Private frameCount As Integer
    Private dimension As FrameDimension
    Private stopImage As Image
    Private isStopped As Boolean

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim f1 As New MemoryStream

        Me.myGif = New Bitmap("strike1.gif")
        Me.dimension = New FrameDimension(Me.myGif.FrameDimensionsList(0))
        Me.frameCount = Me.myGif.GetFrameCount(Me.dimension)

        Me.myGif.SelectActiveFrame(Me.dimension, 3)
        Me.myGif.Save(f1, ImageFormat.Bmp)
        Me.stopImage = Image.FromStream(f1)
        Me.isStopped = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f1, f2, f3, f4 As New MemoryStream

        ' Sets the active frame to index 3
        Me.myGif.SelectActiveFrame(Me.dimension, 3)
        ' Saves the current frame as an image to the memory stream
        Me.myGif.Save(f1, ImageFormat.Bmp)
        ' Loads the image from the stream to the picture box
        Me.PictureBox1.Image = Image.FromStream(f1)

        Me.myGif.SelectActiveFrame(Me.dimension, 1)
        Me.myGif.Save(f2, ImageFormat.Bmp)
        Me.PictureBox2.Image = Image.FromStream(f2)

        Me.myGif.SelectActiveFrame(Me.dimension, 5)
        Me.myGif.Save(f3, ImageFormat.Bmp)
        Me.PictureBox3.Image = Image.FromStream(f3)

        Me.myGif.SelectActiveFrame(Me.dimension, 2)
        Me.myGif.Save(f4, ImageFormat.Bmp)
        Me.PictureBox4.Image = Image.FromStream(f4)

        Me.PictureBox5.Image = Me.myGif
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Me.isStopped Then
            Me.PictureBox5.Image = Me.myGif
            Me.myGif.SelectActiveFrame(Me.dimension, 6)
        Else
            Me.PictureBox5.Image = Me.stopImage
        End If

        Me.isStopped = Not Me.isStopped
    End Sub
End Class

Open in new window

0
chevronrodAuthor Commented:
I believe all of this is going to work. There is an error, though, and i think the solution is a simple one but it eludes me. I get an "invalid parameter" error on this line:

Me.myGif = New Bitmap("strike1.gif")

I have the "strike.gif" file in my Resources as an embedded resource. I'm thinking there is something, maybe, to be added to the path or something....

Can you help me with that?
0
chevronrodAuthor Commented:
Nevermind my last comment--I found it!

I replaced that line with: Me.myGif = my.resources.strike1
0
käµfm³d 👽Commented:
Cool. Glad you got it working and that it was helpful  :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.