0

I have been trying to teach myself C#and in order to teach myself I have been developing a simple game that has two superheroes attack one another using gifs as the animation for the actions.

I have been trying to figure out a work around to have the gif loop once and then set the last image as a still image.

I created a function that extracts the frames and returns an Image array.

    /// <summary>
    /// A method that extracts the frames of a gif.
    /// </summary>
    /// <param name="original_gif">The original gif</param>
    /// <returns>Array of image frames</returns>
    private Image[] GetFrames(Image original_gif)
    {
        ///Get the frame count of the gif
        int number_of_frames = original_gif.GetFrameCount(FrameDimension.Time);

        ///New image array to store the extracted frames
        Image[] frames = new Image[number_of_frames];

        ///Loop through the gif and store each frame in the frames array
        for (int index = 0; index < number_of_frames; index++)
        {
            original_gif.SelectActiveFrame(FrameDimension.Time, index);
            frames[index] = ((Image)original_gif.Clone());
        }

        return frames;
    }

When I try to loop through the Image array and set the last element to the picturebox

    /// <summary>
    /// A method that changes the hero picture box to standing
    /// </summary>
    private void SetStandingImage()
    {
        ///Extract images from the gif
        Image[] frames = GetFrames(Properties.Resources.captain_motion_standing);

        ///Loop through the frames displaying each image once
        for (int index = 0; index < frames.Length; index++)
        {
           ///Set the current image to the hero picture box
            hero_picture_box.Image = frames[index];
        }
        ///Set the last frame of the gif as the hero picture box
        hero_picture_box.Image = frames[frames.Length - 1];
    }

The Image in the picturebox does not get set to the last frame of the array. The picturebox keeps showing the gif animation that is looping.

Even if I comment out the loop and just set the last image to the picturebox it displays the gif that is looping.

How would you extract images from a gif and set the last image to a picturebox?

TurtleMan
  • 175
  • 2
  • 15
  • Did you [see this](https://stackoverflow.com/questions/27874353/how-to-show-a-specific-frame-of-a-gif-image-in-c) ? The active frame doesn't look right, as it will change while the gif displays and setting it restarts the animation. – TaW Jul 27 '18 at 07:40
  • 1
    Thank you for the solution @TaW. Most of over looked that post. Changing `frames[index] = ((Image)original_gif.Clone());` to `frames[index] = new Bitmap(original_gif);` and now have full control over the images in the image array. – TurtleMan Jul 27 '18 at 08:08

0 Answers0