Hide picturebox after GIF is "Played"
- So I'd like to have it so that on the form load it plays and animated gif, then after that gif is done it hides and then shows a different picturebox.
How would I be able to do this?
Could I use some form of timer and count how long the gif lasts and then use a timer for how long it is? If so how explain a bit.
Answers
- You do not need to manage a GIF with a timer when the image file has the layered inages built into it. I have used the Gimp program to create such a GIF successfully
Here's some code that works. Keep in mind that you do not need to micro-manage the frames -- it animates itself simply by setting the Picturebox's Image property. The timer in my example is there to stop the animation -- and to do that you can simply hide it and remove the handler for the animator. This code just requires a blank form with a picturebox dragged on to it.
Public Class Form1 Private WithEvents tmr As New Timer Private Duration As Int32 = 10 'in seconds Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load PictureBox1.Image = My.Resources.MyAnimation 'A multi-image GIF imported into project resources. AddHandler PictureBox1.Paint, AddressOf PictureBox1_Paint_Animate ImageAnimator.Animate(PictureBox1.BackgroundImage, AddressOf OnFrameChanged) 'Note: The gif animates itself - you do not need to micro-manage its frames. tmr.Interval = 1000 tmr.Start() End Sub Private Sub PictureBox1_Paint_Animate(ByVal sender As Object, ByVal e As PaintEventArgs) ImageAnimator.UpdateFrames(PictureBox1.Image) End Sub Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs) 'Force a call to the Paint event handler. PictureBox1.Invalidate() PictureBox1.Refresh() End Sub Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick Static counter As Int32 = 1 counter += 1 'This is showing a hard stop at 10 seconds. If counter >= 10 Then PictureBox1.Hide() tmr.Stop() ImageAnimator.StopAnimate(PictureBox1.Image, AddressOf OnFrameChanged) RemoveHandler PictureBox1.Paint, AddressOf PictureBox1_Paint_Animate End If End Sub End Class
All Replies
- You do not need to manage a GIF with a timer when the image file has the layered inages built into it. I have used the Gimp program to create such a GIF successfully
Here's some code that works. Keep in mind that you do not need to micro-manage the frames -- it animates itself simply by setting the Picturebox's Image property. The timer in my example is there to stop the animation -- and to do that you can simply hide it and remove the handler for the animator. This code just requires a blank form with a picturebox dragged on to it.
Public Class Form1 Private WithEvents tmr As New Timer Private Duration As Int32 = 10 'in seconds Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load PictureBox1.Image = My.Resources.MyAnimation 'A multi-image GIF imported into project resources. AddHandler PictureBox1.Paint, AddressOf PictureBox1_Paint_Animate ImageAnimator.Animate(PictureBox1.BackgroundImage, AddressOf OnFrameChanged) 'Note: The gif animates itself - you do not need to micro-manage its frames. tmr.Interval = 1000 tmr.Start() End Sub Private Sub PictureBox1_Paint_Animate(ByVal sender As Object, ByVal e As PaintEventArgs) ImageAnimator.UpdateFrames(PictureBox1.Image) End Sub Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs) 'Force a call to the Paint event handler. PictureBox1.Invalidate() PictureBox1.Refresh() End Sub Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick Static counter As Int32 = 1 counter += 1 'This is showing a hard stop at 10 seconds. If counter >= 10 Then PictureBox1.Hide() tmr.Stop() ImageAnimator.StopAnimate(PictureBox1.Image, AddressOf OnFrameChanged) RemoveHandler PictureBox1.Paint, AddressOf PictureBox1_Paint_Animate End If End Sub End Class
- What exactly are you talking about with layered images etc? This is just a standard gif file that was made from an AVI.
What exactly are you talking about with layered images etc? This is just a standard gif file that was made from an AVI.
Gif is made of multiple images or graphics objects which means each object is store in different layer and refers as frame
kaymaf
If that what you want, take it. If not, ignored it and no complain- That code is confusing and I don't know how to use it for what I want.
Edit:Nevermind, I got it to work right, thanks! - That is not a particularly good portend for your project, I'm afraid. (The code is not complicated, but...the "big picture" might be. This is a big issue for people learning this stuff; its not just you.)
The code can be divided (functionally, at least) into 3 main parts.
Initialization - The form load function gets called when your application lights off, and you can see that by the appearance of the form. Everything that is done in form load (at least synchronous things) will be completed by the time you see the form. What Dig-boy has done is to load your animation from the resource, and add a handler for the animation (He uses the Paint event.) Finally, he creates and starts the timer.
Paint - You haven't changed anything there, really.
Timer - That function gets called everytime the timer rolls over. It is also where you say that you want to hide that picturebox, and put another in its place...although, why couldn't you just change the image in the already existing and visible one? (You just call Hide on any control, and update it, or refresh the form it is on...that's all there is to hiding a window.)
The static counter is just there so that it can be declared and consumed in one place, and it retain its value, unlike a normal variable in the exact same place, but without the static modifier. (Which would be re-initialized each time...not very useful.)- Edited byjinzai Tuesday, November 03, 2009 10:03 PMtypos
- Thanks Jinzai. I cut the code out of a larger project and wasn't able to clean it up very well, so I apologize Endrien if I made it a little more difficult to understand than it might have been.
Here's a link to the homepage for The Gimp image editing software -- an open source, free program that will allow you to produce animation GIFs.
www.gimp.org
The way I make my animations is by starting with a canvas of the desired size (say, 64x64) then adding a new layer for each of the frames of the animation. Remove any extra layers you may have incurred, then save it as a GIF. There are options during save that influence how the animation looks and works, so read up on the documentation before bulling your way through it. Keep in mind that you can have transparency within the image layers and they will be preserved in the final GIF.
Give it a try! Thanks Jinzai. I cut the code out of a larger project and wasn't able to clean it up very well, so I apologize Endrien if I made it a little more difficult to understand than it might have been.
Here's a link to the homepage for The Gimp image editing software -- an open source, free program that will allow you to produce animation GIFs.
www.gimp.org
The way I make my animations is by starting with a canvas of the desired size (say, 64x64) then adding a new layer for each of the frames of the animation. Remove any extra layers you may have incurred, then save it as a GIF. There are options during save that influence how the animation looks and works, so read up on the documentation before bulling your way through it. Keep in mind that you can have transparency within the image layers and they will be preserved in the final GIF.
Give it a try!
Sorry if I sounded like a noob to it, I actually am fairly good at animating GIF's in photoshop etc, I just didn't quite get what you said haha.- Disregard then! You could explain it better than me. :)


