共用方式為


Animated Gifs in .NET

I hadn't noticed that the graphic classes in .NET had built-in support for animated gifs, but a recent GDN post shows how you can grab the individual frames out of a GIF and display them one at a time... I think you would have to find out the intended delay between frames to produce the author's desired result, but I'm not positive as it has been awhile since I made an animated gif :)

<www.gotdotnet.com/Community/MessageBoard/Thread.aspx?id=180291>

Comments

  • Anonymous
    January 12, 2004
    Windows Forms has built-in support for GIF animation via the ImageAnimator class, e.g.

    // Load animated GIF
    Bitmap gif = new Bitmap(@"C:SAMPLE_ANIMATION_COPY.GIF");

    void AnimationForm_Load(object sender, EventArgs e) {
    // Check if image supports animation
    if( ImageAnimator.CanAnimate(gif) ) {
    // Subscribe to event indicating the next frame should be shown
    ImageAnimator.Animate(gif, new EventHandler(gif_FrameChanged));
    }
    }

    void gif_FrameChanged(object sender, EventArgs e) {
    if( this.InvokeRequired ) {
    // Transition from worker thread to UI thread
    this.BeginInvoke(
    new EventHandler(gif_FrameChanged),
    new object[] { sender, e });
    }
    else {
    // Trigger Paint event to draw next frame
    this.Invalidate();
    }
    }

    void AnimationForm_Paint(object sender, PaintEventArgs e) {
    // Update image's active frame
    ImageAnimator.UpdateFrames(gif);

    // Draw image's current frame
    Graphics g = e.Graphics;
    g.DrawImage(gif, this.ClientRectangle);
    }

    This is covered in Chapter 4 of Windows Forms Programming: http://sellsbrothers.com/writing/wfbook
  • Anonymous
    January 12, 2004
    The comment has been removed
  • Anonymous
    January 23, 2004
    Duh!
    Just Place the Animated Gif in a picturebox on the form and let it handle all the animation - automatically!
  • Anonymous
    February 25, 2004
    Rama,

    The request was to play the animation exactly one time then stop.

    Placed in a PictureBox this control is not possible.

    Mike