Results 1 to 9 of 9

Thread: Loop Once Through an Animated GIF

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2001
    Posts
    16

    Loop Once Through an Animated GIF

    I'm using a picture box to show an animated gif on my form. When i created the gif, I set it so it only loops once. The gif plays correctly in a browser, but when i set the image of the picture box to my animated gif it continuously loops.

    I only want to loop once through the images embedded in my animated gif when i set the image of the picture box. What is the easiest way to do this? Does anyone have or know of any sample code to do this? I have been searching this for a while, but have not been able to find anything.

    Thanks in advance.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Loop Once Through an Animated GIF

    Try the ImageAnimator class.

  3. #3
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Loop Once Through an Animated GIF

    ImageAnimator unfortunately doesn't have a convenient "play once" method. Maybe it's not the most elegant solution, but I found this works:
    vb.net Code:
    1. Private GIFAnim As Image = Image.FromFile(--your animated GIF file--)
    2.     Private frames As Integer
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         frames = GIFAnim.GetFrameCount(Imaging.FrameDimension.Time)
    6.         ImageAnimator.Animate(GIFAnim, AddressOf paintFrame)
    7.     End Sub
    8.  
    9.     Private Sub paintFrame(ByVal sender As Object, ByVal e As EventArgs)
    10.         If frames > 0 Then PictureBox1.Invalidate() Else ImageAnimator.StopAnimate(GIFAnim, AddressOf StopAnim)
    11.     End Sub
    12.  
    13.     Private Sub StopAnim(ByVal sender As Object, ByVal e As EventArgs)
    14.         PictureBox1.Invalidate()
    15.     End Sub
    16.  
    17.     Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    18.         If frames > 0 Then
    19.             ImageAnimator.UpdateFrames()
    20.             e.Graphics.DrawImage(GIFAnim, Point.Empty)
    21.             frames -= 1
    22.         End If
    23.     End Sub

    BB

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2001
    Posts
    16

    Re: Loop Once Through an Animated GIF

    Sorry, I missed this response. I will give it a try. Thanks much.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2001
    Posts
    16

    Re: Loop Once Through an Animated GIF

    That works great. The only problem is it freezes the image at the 1st frame as opposed to the 3rd frame. But I should be able to figure that out.

  6. #6
    New Member
    Join Date
    Sep 2013
    Location
    Berlin, Germany
    Posts
    2

    Re: Loop Once Through an Animated GIF

    hi boobs boobs, I am an absolute newbie!
    I am using Visual Basic 2010 Express.
    I wrote a tiny program that only plays a gif-animation and a background sound. Of course, I have the same problem that the animation is set to loop.
    My code:
    Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    My.Computer.Audio.Play(My.Resources.sound_1, AudioPlayMode.Background)
    End Sub

    End Class

    I have tested your code - it works without problems.
    But I have the problem to integrate it into my project (or conversely), because I don't have the know how..
    Please help me:
    I want, as possible, the animation and .wav load from a resource (integrated in .exe after compile). On startup the animation should run without click button (no button).

    thanks Tom

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Loop Once Through an Animated GIF

    You can load and run the Gif from Resources without any difficulty. Simply replace GIFAnim with the reference to your resource Gif wherever it appears. To run the animation unprompted move all the code in the Button1.Click handler to a Form1.Shown handler.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8
    New Member
    Join Date
    Sep 2013
    Location
    Berlin, Germany
    Posts
    2

    Re: Loop Once Through an Animated GIF

    Thank you, but I'm to stupid.
    My code now:
    Name:  Ashampoo_Snap_2013.09.20_14h01m32s_001_.jpg
Views: 3426
Size:  214.3 KB

    I don't know how I can change the code for use the Gif from Resource and what I can do for center the image in the form and in the PictureBox?
    I try alot but - what I write in the first line!
    Please Help...

  9. #9
    New Member
    Join Date
    Feb 2014
    Posts
    1

    Re: Loop Once Through an Animated GIF

    Hi There, You got working how to use Gif from Resource ? Im stucked at this point too, i really appreciate your answer. Im waiting if u can help me
    Quote Originally Posted by Tom_Berlin View Post
    Thank you, but I'm to stupid.
    My code now:
    Name:  Ashampoo_Snap_2013.09.20_14h01m32s_001_.jpg
Views: 3426
Size:  214.3 KB

    I don't know how I can change the code for use the Gif from Resource and what I can do for center the image in the form and in the PictureBox?
    I try alot but - what I write in the first line!
    Please Help...

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