0

I am coding using Visual Basic. I have a little problem however.

For my program, I need to layer GIFs/PNGs. I have accomplished this by using .DrawImage() It works fine however, the GIFs are inanimate and static.

I have tried using the ImageAnimator Class, however, I am a bit in the dark. I tried using the sample code from MSDN, and it is not working for me. This is probably because I don't quite understand it.

Private animatedImage As New Bitmap(My.Resources._a_takingnotes)
Private currentlyAnimating As Boolean = False


Public Sub AnimateImage()
    If Not currentlyAnimating Then

        'Begin the animation only once.
        ImageAnimator.Animate(animatedImage, _
        New EventHandler(AddressOf Me.OnFrameChanged))
        currentlyAnimating = True
    End If
End Sub

Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)

    'Force a call to the Paint event handler. 
    Me.Invalidate()
End Sub

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

    'Begin the animation.
    AnimateImage()

    'Get the next frame ready for rendering.
    ImageAnimator.UpdateFrames()

    'Draw the next frame in the animation.
    e.Graphics.DrawImage(Me.animatedImage, New Point(0, 0))
    PictureBox2.Image = Nothing
    PictureBox2.Image = animatedImage
End Sub

The above is the code that is supposed to animate the "(a)takingnotes.gif" (called as a resource).

It returns a static image of the first frame in the PictureBox.

Thank you!

EDIT: AnimateImage() is called on Load, which didn't work. I also tested on every Timer Tick, which also didn't work.

michal
  • 226
  • 2
  • 11
  • a picturebox will display gif animation automatically: `PictureBox1.Image = My.Resources.ANIM_GIF_REF` – Ňɏssa Pøngjǣrdenlarp Mar 24 '15 at 15:21
  • @Plutonix Yes, however, since I am layering multiple photos, I need an animated bitmap. If I just sent the gif to a picture box, there would be problems with transparency. I will see if I can nonetheless. – michal Mar 24 '15 at 15:24
  • 1
    What happens if you delete the `PictureBox2.Image = [...]` lines and add the second of them after `currentlyAnimating = True`? – Georg Jung Mar 24 '15 at 15:59
  • 1
    I believe you do not want to override the forms `OnPaint` method but rather write your code in the Paint event of your PictureBox. With the code you posted you try to paint your animation on your form, not in the picture box. – Georg Jung Mar 24 '15 at 16:02
  • @GeorgJung I have solved the problem. Apparently, when I put my .gif files into the resources, for some reason it deletes the anim. It still works with PictureBox elements, which confused me. However, after calling a .gif that I put in AFTER deployment, the anim works successfully. – michal Mar 24 '15 at 16:30
  • Then try importing the gif ressource as file, not as image. – Georg Jung Mar 24 '15 at 16:35
  • https://stackoverflow.com/questions/67300278/how-to-play-a-gif-animation-to-the-last-frame-then-stop-the-animation/67305196?noredirect=1#comment119032825_67305196 – Mugisha May 01 '21 at 16:40

0 Answers0