代码之家  ›  专栏  ›  技术社区  ›  Simon Randy Burden

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

  •  4
  • Simon Randy Burden  · 技术社区  · 15 年前

    如何将动画GIF拆分为.NET中的组件?

    具体来说,我想把它们载入内存中的图像(system.drawing.image)。

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

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

    public static IEnumerable<Bitmap> GetImages(Stream stream)
    {
        using (var gifImage = Image.FromStream(stream))
        {
            var dimension = new FrameDimension(gifImage.FrameDimensionsList[0]); //gets the GUID
            var frameCount = gifImage.GetFrameCount(dimension); //total frames in the animation
            for (var index = 0; index < frameCount; index++)
            {
                gifImage.SelectActiveFrame(dimension, index); //find the frame
                yield return (Bitmap) gifImage.Clone(); //return a copy of it
            }
        }
    }
    
    4 回复  |  直到 7 年前
        1
  •  3
  •   ДМИТРИЙ МАЛИКОВ    7 年前

    使用 SelectActiveFrame 方法选择 Image 拿着动画gif的实例。例如:

    image.SelectActiveFrame(FrameDimension.Time, frameIndex);
    

    要获取帧数,请调用 GetFrameCount(FrameDimension.Time)

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

        2
  •  2
  •   Neoheurist    10 年前
    // 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;
    }
    
        3
  •  0
  •   Frank Krueger    15 年前

    半相关的,在wpf中,您有位图解码器,它将为您提供图像的所有帧。

    BitmapDecoder.Create BitmapDecoder.Frames .

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