コード例 #1
0
        /// <summary>
        ///     对gif图像文件进行解码
        /// </summary>
        /// <param name="gifPath">gif文件路径</param>
        internal static GifImage Decode(string gifPath)
        {
            using (var stream = new GifStream(new FileStream(gifPath, FileMode.Open)))
            {
                var gifImage   = new GifImage();
                var graphics   = new List <GraphicEx>();
                var frameCount = 0;

                //读取文件头

                //开始6字节是GIF署名 文件版本号,GIF署名 是“GIF”,文件版本号为 "87a"或"89a"
                //87a 是 1987 版,89a 是 1989 提出的,所以就需要跳过 6 字节
                gifImage.Header = stream.ReadString(6);
                //读取逻辑屏幕标示符
                gifImage.LogicalScreenDescriptor = stream.GetLogicalScreenDescriptor();
                if (gifImage.LogicalScreenDescriptor.GlobalColorTableFlag)
                {
                    //读取全局颜色列表
                    gifImage.GlobalColorTable =
                        stream.ReadByte(gifImage.LogicalScreenDescriptor.GlobalColorTableSize * 3);
                }

                int nextFlag;
                while ((nextFlag = stream.Read()) != GifExtensions.Terminator)
                {
                    if (nextFlag == GifExtensions.ImageLabel)
                    {
                        ReadImage(stream, gifImage, graphics, frameCount);
                        frameCount++;
                    }
                    else if (nextFlag == GifExtensions.ExtensionIntroducer)
                    {
                        var gcl = stream.Read();
                        switch (gcl)
                        {
                        case GifExtensions.GraphicControlLabel:
                        {
                            var graphicEx = stream.GetGraphicControlExtension();
                            graphics.Add(graphicEx);
                            break;
                        }

                        case GifExtensions.CommentLabel:
                        {
                            var comment = stream.GetCommentEx();
                            gifImage.CommentExtensions.Add(comment);
                            break;
                        }

                        case GifExtensions.ApplicationExtensionLabel:
                        {
                            var applicationEx = stream.GetApplicationEx();
                            gifImage.ApplictionExtensions.Add(applicationEx);
                            break;
                        }

                        case GifExtensions.PlainTextLabel:
                        {
                            var textEx = stream.GetPlainTextEx();
                            gifImage.PlainTextExtensions.Add(textEx);
                            break;
                        }
                        }
                    }
                    else if (nextFlag == GifExtensions.EndIntroducer)
                    {
                        //到了文件尾
                        break;
                    }
                }

                return(gifImage);
            }
        }