我一直在自学C#
,为了自学,我一直在开发一个简单的游戏,其中有两个超级英雄使用gif动画作为动作来互相攻击。
我一直在试图解决一个问题,使gif
循环一次,然后将最后一张图像设置为静止图像。
我创建了一个提取帧并返回Image
数组的函数。
/// <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;
}
当我尝试遍历Image
数组并将最后一个元素设置为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];
}
Image
中的picturebox
未设置为数组的最后一帧。 picturebox
继续显示循环的gif动画。
即使我注释掉循环并将刚刚的图像设置为picturebox
,它也会显示正在循环的gif。
您将如何从gif中提取图像并将最后一个图像设置为picturebox
?