public void Save(Stream aStream)
        {
            this.BehaviorExtention.Save(aStream);

            foreach (var _extention in this.Extensions)
            {
                _extention.Save(aStream);
            }

            aStream.WriteByte(Separator);

            var _bw = new BinaryWriter(aStream);

            _bw.Write(this.FOffsetX);
            _bw.Write(this.FOffsetY);
            _bw.Write((UInt16)this.Width);
            _bw.Write((UInt16)this.Height);
            _bw.Write(this.ImageFlags);

            if (!this.UseGlobalPalette)
            {
                foreach (var _c in this.FFrame.Palette.Colors)
                {
                    _bw.Write(_c.R);
                    _bw.Write(_c.G);
                    _bw.Write(_c.B);
                }
            }

            var _gifEncoder = new GifBitmapEncoder();

            _gifEncoder.Frames.Add(this.FFrame);

            var _buff = new MemoryStream();

            _gifEncoder.Save(_buff);

            // find image data start position
            var _fileHeaderSize = 13;

            _buff.Seek(_fileHeaderSize, SeekOrigin.Begin);

            GifExtension _ext = null;

            while ((_ext = GifExtension.Load(_buff)) != null)
            {
                ;
            }
            var _imageDataStart = (int)_buff.Position;

            var _data = _buff.ToArray();

            _buff.Close();

            var _imageHeaderSize        = 9;
            int _imageDataStartPosition = _imageDataStart + _imageHeaderSize + this.FFrame.Palette.Colors.Count * 3;

            _bw.Write(_data, _imageDataStartPosition, _data.Length - _imageDataStartPosition - 1);
        }
Exemple #2
0
        public void Load(Stream aStream)
        {
            var _decoder = new GifBitmapDecoder(aStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);

            aStream.Position = 0;
            var _formatIdData = new byte[FormatId.Length];

            aStream.Read(_formatIdData, 0, _formatIdData.Length);
            var _br = new BinaryReader(aStream);

            this.FScreenWidth  = _br.ReadUInt16();
            this.FScreenHeight = _br.ReadUInt16();
            int _flags = _br.ReadByte();

            this.FBackgroundColorIndex = _br.ReadByte();
            aStream.ReadByte();                                                                         // skip pixel shape

            if ((_flags & 0x80) != 0)
            {
                int _bitsPerPixel  = (_flags & 0x07) + 1;
                int _paletteLength = (int)Math.Pow(2, _bitsPerPixel);
                var _colors        = new List <Color>(_paletteLength);

                for (int i = 0; i < _paletteLength; i++)
                {
                    var _c = new Color();
                    _c.R = _br.ReadByte();
                    _c.G = _br.ReadByte();
                    _c.B = _br.ReadByte();
                    _colors.Add(_c);
                }

                this.FGlobalPalette = new BitmapPalette(_colors);
            }

            GifExtension _extension = null;

            while ((_extension = GifExtension.Load(aStream)) != null &&
                   _extension.ExtensionType != ExtensionType.ImageBehaviorExtension)
            {
                this.Extensions.Add(_extension);
            }

            for (int i = 0; i < _decoder.Frames.Count; i++)
            {
                var _currentFrame = new GifBitmapFrame(_decoder.Frames[i]);
                _currentFrame.BehaviorExtention = _extension;
                _extension = _currentFrame.Load(aStream);
                this.Frames.Add(_currentFrame);
            }
        }
        public GifExtension Load(Stream aStream)
        {
            GifExtension _extension = null;

            while ((_extension = GifExtension.Load(aStream)) != null)
            {
                this.Extensions.Add(_extension);
            }

            var _br = new BinaryReader(aStream);

            this.FOffsetX = _br.ReadUInt16();
            this.FOffsetY = _br.ReadUInt16();
            _br.ReadUInt16();                                                   // don't store width, get from frame
            _br.ReadUInt16();                                                   // don't store heiht, get from frame
            this.ImageFlags = _br.ReadByte();
            // palette stored in frame
            if (!this.UseGlobalPalette)
            {
                aStream.Seek(this.FFrame.Palette.Colors.Count * 3, SeekOrigin.Current);
            }

            _br.ReadByte();                                                     // skip code size

            // image data stored in frame
            var _dataLength = _br.ReadByte();

            while (_dataLength != 0)
            {
                aStream.Seek(_dataLength, SeekOrigin.Current);
                _dataLength = _br.ReadByte();
            }

            while ((_extension = GifExtension.Load(aStream)) != null &&
                   _extension.ExtensionType != ExtensionType.ImageBehaviorExtension)
            {
                this.Extensions.Add(_extension);
            }

            return(_extension);
        }
        public static GifExtension Load(Stream aStream)
        {
            var _br = new BinaryReader(aStream);

            if (_br.ReadByte() != Separator)
            {
                return(null);
            }

            var          _extensionType = (ExtensionType)_br.ReadByte();
            GifExtension _extension     = null;

            switch (_extensionType)
            {
            case ExtensionType.ApplicationExtension:
            {
                _extension = new GifApplicationExtension();
                break;
            }

            case ExtensionType.CommentExtension:
            {
                _extension = new GifCommentExtension();
                break;
            }

            case ExtensionType.ImageBehaviorExtension:
            {
                _extension = new GifExtension(_extensionType);
                break;
            }
            }
            _extension.FExtensionType = _extensionType;
            _extension.LoadData(aStream);
            return(_extension);
        }