Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在.net中拆分动画gif?_.net_Image_Image Manipulation_File Format_Gif - Fatal编程技术网

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

如何在.net中拆分动画gif?,.net,image,image-manipulation,file-format,gif,.net,Image,Image Manipulation,File Format,Gif,如何在.net中将动画gif拆分为其组件 具体来说,我想将它们加载到内存中的Image(System.Drawing.Image)中 ====================== 根据SLaks的回答,我现在有了这个 public static IEnumerable<Bitmap> GetImages(Stream stream) { using (var gifImage = Image.FromStream(stream)) { //gets t

如何在.net中将动画gif拆分为其组件

具体来说,我想将它们加载到内存中的Image(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();
        }
    }
}
公共静态IEnumerable GetImages(流)
{
使用(var gifImage=Image.FromStream(stream))
{
//获取GUID
var-dimension=新的FrameDimension(gifImage.FrameDimensionsList[0]);
//动画中的总帧数
var frameCount=gifImage.GetFrameCount(维度);
对于(变量索引=0;索引<帧数;索引++)
{
//找到框架
gifImage.SelectActiveFrame(维度、索引);
//还一份
产生返回(位图)gifImage.Clone();
}
}
}
使用该方法选择包含动画GIF的
图像
实例的活动帧。例如:

image.SelectActiveFrame(FrameDimension.Time, frameIndex);
要获取帧数,请调用
GetFrameCount(FrameDimension.Time)


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

半相关,在WPF中,您可以使用位图解码器为您提供图像的所有帧

请参阅和。

//将单个位图帧从多帧位图解析为位图数组
私有位图[]解析帧(位图动画)
{
//获取要复制到位图数组中的动画帧数
int Length=Animation.GetFrameCount(FrameDimension.Time);
//分配位图数组以保存动画中的各个帧
位图[]帧=新位图[长度];
//将动画位图帧复制到位图数组中
for(int Index=0;Index
完成后,应使用块将代码包装在
中,以处理图像。谢谢。更新:)这项技术(用
C##
编写)比
Clone()
方法有优势,因为
Clone()
方法复制每个帧的整个动画,基本上等于存储所有帧(例如
Clone()所需的内存量的平方
方法生成一组动画,其中每个副本具有不同的当前帧)。为了真正解析动画帧,需要将每个动画帧绘制到数组中自己的
位图中。根据需要,一旦动画被解析到帧数组中,它就可以被垃圾收集。。。
// 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;
}
Image img = Image.FromFile(@"D:\images\zebra.gif");
//retrieving 1st frame
 img.SelectActiveFrame(new FrameDimension(img.FrameDimensionsList[0]), 1);
 pictureBox1.Image = new Bitmap(img);