• 大小: 71KB
    文件类型: .cs
    金币: 1
    下载: 0 次
    发布日期: 2021-06-04
  • 语言: C#
  • 标签: c#  gif  制作  如何    

资源简介

c# 使用图片制作 gif 的时候,需要用到一个类,那就是 AnimatedGifEncoder,使用的时候,把这个类复制进工程中,就可以使用相关类了。

资源截图

代码片段和文件信息

using System;
using System.Collections;
using System.Drawing;
using System.IO;

namespace 你自己的命名空间
{
    //我使用的是 vs2017 .net framework 4.0
    internal class AnimatedGifEncoder
    {


        protected int width; // image size
        protected int height;
        protected Color transparent = Color.Empty; // transparent color if given
        protected int transIndex; // transparent index in color table
        protected int repeat = -1; // no repeat
        protected int delay = 0; // frame delay (hundredths)
        protected bool started = false; // ready to output frames
                                        //      protected BinaryWriter bw;
        protected FileStream fs;

        protected Image image; // current frame
        protected byte[] pixels; // BGR byte array from frame
        protected byte[] indexedPixels; // converted frame indexed to palette
        protected int colorDepth; // number of bit planes
        protected byte[] colorTab; // RGB palette
        protected bool[] usedEntry = new bool[256]; // active palette entries
        protected int palSize = 7; // color table size (bits-1)
        protected int dispose = -1; // disposal code (-1 = use default)
        protected bool closeStream = false; // close stream when finished
        protected bool firstframe = true;
        protected bool sizeSet = false; // if false get size from first frame
        protected int sample = 10; // default sample interval for quantizer

        /**
         * Sets the delay time between each frame or changes it
         * for subsequent frames (applies to last frame added).
         *
         * @param ms int delay time in milliseconds
         */
        public void SetDelay(int ms)
        {
            delay = (int)System.Math.Round(ms / 10.0f);
        }

        /**
         * Sets the GIF frame disposal code for the last added frame
         * and any subsequent frames.  Default is 0 if no transparent
         * color has been set otherwise 2.
         * @param code int disposal code.
         */
        public void SetDispose(int code)
        {
            if (code >= 0)
            {
                dispose = code;
            }
        }

        /**
         * Sets the number of times the set of GIF frames
         * should be played.  Default is 1; 0 means play
         * indefinitely.  Must be invoked before the first
         * image is added.
         *
         * @param iter int number of iterations.
         * @return
         */
        public void SetRepeat(int iter)
        {
            if (iter >= 0)
            {
                repeat = iter;
            }
        }

        /**
         * Sets the transparent color for the last added frame
         * and any subsequent frames.
         * Since all colors are subject to modification
         * in the quantization process the color in the final
         * palette for each fram

评论

共有 条评论