使用dotimage调整动画gif的大小

时间:2011-07-18 11:45:13

标签: c# graphics animated-gif dotimage

我有一段代码可以调整GIF动画的大小。 如果它有助于代码将始终将图像调整为较小的大小。 (现在没有必要让它们变大)

我正在使用Atalasoft的dotimage库及其示例代码来进行实际重新采样。 该代码应该从磁盘读取动画gif,迭代帧并将每个帧的大小调整为新的大小。 当gif动画包含相同大小的帧时,这可以正常工作,但是调整具有不同大小的帧的动画会破坏动画(在调整大小后帧不会正确地相互重叠),我认为这是因为代码不计算新的偏移量正确。

我认为正是这行代码没有正确计算偏移量:                     Point point = new Point((int)(frame.Location.X * ratio),(int)(frame.Location.Y * ratio));

这是完整的调整大小例程:

    static private void GenerateGifImage(FileStream fileStream, int OutputWidth, int OutputHeight)
    {            
        // MemoryStream InputStream = new MemoryStream();
        FileStream InputStream = fileStream;
        // fileStream.Write(InputStream.GetBuffer(), 0, (int)InputStream.Position);
        // InputStream.Seek(0, SeekOrigin.Begin);
        Image InputImage = Image.FromStream(InputStream, true, false);

        // this will invalidate the underlying image object in InputImage but the class properties 
        // will still accessible until the object is disposed
        InputStream.Seek(0, SeekOrigin.Begin);

        ImageInfo imageInfo = RegisteredDecoders.GetImageInfo(InputStream);
        InputStream.Seek(0, SeekOrigin.Begin);

        GifDecoder gifDecoder = new GifDecoder();
        int count = gifDecoder.GetFrameCount(InputStream);

        GifFrameCollection gifFrameCollection = new GifFrameCollection();
        gifFrameCollection.Height = OutputHeight;
        gifFrameCollection.Width = OutputWidth;
        // gifFrameCollection.Height = gifDecoder.Frames.Height;
        // gifFrameCollection.Width = gifDecoder.Frames.Width;

        double ratio;
        if (InputImage.Height > InputImage.Width)
        {
            ratio = (double)OutputHeight / (double)InputImage.Height;
        }
        else
        {
            ratio = (double)OutputWidth / (double)InputImage.Width;
        }

        for (int i = 0; i < count; i++)
        {
            GifFrame frame = gifDecoder.Frames[i];

            Rectangle rectangle = new Rectangle(Point.Empty, frame.Image.Size);

            int frameWidth = (int)(frame.Image.Width * ratio);
            int frameHeight = (int)(frame.Image.Height * ratio);

            // account for erratic rounding, seems illogical but has happened earlier when using floats instead of doubles 
            if (frameWidth > OutputWidth)
            {
                frameWidth = OutputWidth;
            }
            if (frameHeight > OutputHeight)
            {
                frameHeight = OutputHeight;
            }

            Size size = new Size(frameWidth, frameHeight);
            // only resize if we have a measureable dimension
            if (size.Width > 0 && size.Height > 0)
            {
                // ResampleCommand resampleCommand = new ResampleCommand(rectangle, size, ResampleMethod.NearestNeighbor);
                ResampleCommand resampleCommand = new ResampleCommand(rectangle, size, ResampleMethod.NearestNeighbor);
                AtalaImage atalaImage = resampleCommand.Apply(frame.Image).Image;
                // save the image for debugging
                // atalaImage.Save("frame" + i.ToString() + ".gif", ImageType.Gif, null);
                // frame.Image.Save("frame-orig" + i.ToString() + ".gif", ImageType.Gif, null);

                // AtalaImage atalaImage = frame.Image;
                Point point = new Point((int)(frame.Location.X * ratio), (int)(frame.Location.Y * ratio));
                // Point point = new Point((int)(frame.Location.X), (int)(frame.Location.Y));
                gifFrameCollection.Add(new GifFrame(atalaImage, point, frame.DelayTime, frame.Interlaced, frame.FrameDisposal, frame.TransparentIndex, frame.UseLocalPalette));
            }
        }
        FileStream saveStream = new FileStream("resized.gif", FileMode.Create, FileAccess.Write, FileShare.Write);
        GifEncoder gifSave = new GifEncoder();
        gifSave.Save(saveStream, gifFrameCollection, null);
        saveStream.Close();
    }

2 个答案:

答案 0 :(得分:1)

我在Atalasoft工作

我调查了这个 - 你的代码是完全正确的,并且可以在不同大小的帧上工作。你计算的点是正确的。

问题在于,在你的3帧GIF中,你的第二帧和第三帧被精确地叠加在第一帧之上,并使用非常复杂的透明蒙版来显示第一帧。当您的图像重新采样到新的大小时,蒙版可能不再精确 - 因为您要调整宽度和高度上只有一个像素差异,所以此蒙版无法匹配。

这个问题有几种解决方案

  1. 将第2帧叠加到第1帧上,然后重新取样并使用该图像
  2. 执行#1,但随后提取第2帧的矩形
  3. 使用crop而不是resample - 这看起来最好,因为它只有1个像素。
  4. 我为你编写了#3 - 它运作良好

        static private void GenerateGifImage(FileStream fileStream, int OutputWidth, int OutputHeight)
        {            
            // MemoryStream InputStream = new MemoryStream();
            FileStream InputStream = fileStream;
            // fileStream.Write(InputStream.GetBuffer(), 0, (int)InputStream.Position);
            // InputStream.Seek(0, SeekOrigin.Begin);
            Image InputImage = Image.FromStream(InputStream, true, false);
    
            // this will invalidate the underlying image object in InputImage but the class properties 
            // will still accessible until the object is disposed
            InputStream.Seek(0, SeekOrigin.Begin);
    
            ImageInfo imageInfo = RegisteredDecoders.GetImageInfo(InputStream);
            InputStream.Seek(0, SeekOrigin.Begin);
    
            GifDecoder gifDecoder = new GifDecoder();
            int count = gifDecoder.GetFrameCount(InputStream);
    
            GifFrameCollection gifFrameCollection = new GifFrameCollection();
            gifFrameCollection.Height = OutputHeight;
            gifFrameCollection.Width = OutputWidth;
    
            double ratio;
            if (InputImage.Height > InputImage.Width)
            {
                ratio = (double)OutputHeight / (double)InputImage.Height;
            }
            else
            {
                ratio = (double)OutputWidth / (double)InputImage.Width;
            }
    
            for (int i = 0; i < count; i++)
            {
                GifFrame frame = gifDecoder.Frames[i];
    
                Rectangle rectangle = new Rectangle(Point.Empty, frame.Image.Size);
    
                int newframeWidth = frame.Image.Width;
                int newframeHeight = frame.Image.Height;
                if (newframeWidth > OutputWidth || newframeHeight > OutputHeight)
                {
                    newframeWidth = (int)(frame.Image.Width * ratio);
                    newframeHeight = (int)(frame.Image.Height * ratio);
                }
    
                // account for erratic rounding, seems illogical but has happened earlier when using floats instead of doubles 
                if (newframeWidth > OutputWidth)
                {
                    newframeWidth = OutputWidth;
                }
                if (newframeHeight > OutputHeight)
                {
                    newframeHeight = OutputHeight;
                }
    
                Size size = new Size(newframeWidth, newframeHeight);
                // only resize if we have a measureable dimension
                if (size.Width > 0 && size.Height > 0)
                {
                    //ResampleCommand resampleCommand = new ResampleCommand(rectangle, size, ResampleMethod.);
                    AtalaImage atalaImage = frame.Image;
                    if (newframeWidth != frame.Image.Width || newframeHeight != frame.Image.Height)
                    {
                        CropCommand command = new CropCommand(new Rectangle(new Point(0, 0), size));
                        atalaImage = command.Apply(frame.Image).Image;
                    }
                    // AtalaImage atalaImage = frame.Image;
                    Point point = new Point((int)(frame.Location.X), (int)(frame.Location.Y));
                    // Point point = new Point((int)(frame.Location.X), (int)(frame.Location.Y));
                    gifFrameCollection.Add(new GifFrame(atalaImage, point, frame.DelayTime, frame.Interlaced, frame.FrameDisposal, frame.TransparentIndex, frame.UseLocalPalette));
                }
            }
            FileStream saveStream = new FileStream("resized.gif", FileMode.Create, FileAccess.Write, FileShare.Write);
            GifEncoder gifSave = new GifEncoder();
            gifSave.Save(saveStream, gifFrameCollection, null);
            saveStream.Close();
        }
    

答案 1 :(得分:0)

如果您使用不同的帧大小,则计算的比率值不正确。您应该计算每个帧的比率,以便您关注的线使用正确的比率。我不熟悉这个框架,所以我不能给你一个准确的例子;但它看起来应该类似于:

GifFrame frame = gifDecoder.Frames[i];
double frameRatio;
if (frame.Height > frame.Width)
{
   frameRatio = (double)OutputHeight / (double)frame.Height;
}
else
{
   frameRatio = (double)OutputWidth / (double)frame.Width;
}

...

Point point = new Point((int)(frame.Location.X * frameRatio), (int)(frame.Location.Y * frameRatio));