如何在.net中拆分动画gif?

时间:2009-11-11 02:54:33

标签: .net image image-manipulation file-format gif

如何将动画gif拆分为.net中的组件部分?

具体来说,我想将它们加载到内存中的Image(s)(System.Drawing.Image)中。

======================

根据SLaks的回答,我现在有了这个

public static IEnumerable<Bitmap> GetImages(Stream stream)
{
    using (var gifImage = Image.FromStream(stream))
    {
        //gets the GUID
        var dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);
        //total frames in the animation
        var frameCount = gifImage.GetFrameCount(dimension); 
        for (var index = 0; index < frameCount; index++)
        {
            //find the frame
            gifImage.SelectActiveFrame(dimension, index);
            //return a copy of it
            yield return (Bitmap) gifImage.Clone();
        }
    }
}

4 个答案:

答案 0 :(得分:4)

使用SelectActiveFrame方法选择包含动画GIF的Image实例的有效帧。例如:

image.SelectActiveFrame(FrameDimension.Time, frameIndex);

要获取帧数,​​请致电GetFrameCount(FrameDimension.Time)

如果您只想播放动画,可以将其放入PictureBox或使用ImageAnimator类。

答案 1 :(得分:2)

// Parses individual Bitmap frames from a multi-frame Bitmap into an array of Bitmaps

private Bitmap[] ParseFrames(Bitmap Animation)
{
    // Get the number of animation frames to copy into a Bitmap array

    int Length = Animation.GetFrameCount(FrameDimension.Time);

    // Allocate a Bitmap array to hold individual frames from the animation

    Bitmap[] Frames = new Bitmap[Length];

    // Copy the animation Bitmap frames into the Bitmap array

    for (int Index = 0; Index < Length; Index++)
    {
        // Set the current frame within the animation to be copied into the Bitmap array element

        Animation.SelectActiveFrame(FrameDimension.Time, Index);

        // Create a new Bitmap element within the Bitmap array in which to copy the next frame

        Frames[Index] = new Bitmap(Animation.Size.Width, Animation.Size.Height);

        // Copy the current animation frame into the new Bitmap array element

        Graphics.FromImage(Frames[Index]).DrawImage(Animation, new Point(0, 0));
    }

    // Return the array of Bitmap frames

    return Frames;
}

答案 2 :(得分:0)

半相关,在WPF中你有BitmapDecoders,它将为你提供图像的所有帧。

请参阅BitmapDecoder.CreateBitmapDecoder.Frames

答案 3 :(得分:0)

Image img = Image.FromFile(@"D:\images\zebra.gif");
//retrieving 1st frame
 img.SelectActiveFrame(new FrameDimension(img.FrameDimensionsList[0]), 1);
 pictureBox1.Image = new Bitmap(img);