コード例 #1
0
        /// <summary>
        /// Saves an image to disk.
        /// <para>Can throw just about any exception and should be used in a try catch</para>
        /// </summary>
        /// <param name="stream">The stream to copy to.</param>
        /// <exception cref="Exception"></exception>
        public void Save(Stream stream)
        {
            GifDecoder decoder = new GifDecoder(this.Image);
            GifEncoder encoder = new GifEncoder(decoder.LoopCount);

            for (int i = 0; i < decoder.FrameCount; i++)
            {
                using (GifFrame frame = decoder.GetFrame(i))
                {
                    encoder.EncodeFrame(frame);
                }
            }

            encoder.EncodeToStream(stream);
        }
コード例 #2
0
        /// <summary>
        /// Decode a gif, apply the given <see cref="RotateFlipType"/> and return the re-encoded the gif.
        /// </summary>
        /// <param name="bmp">The gif to process.</param>
        /// <param name="rotateFlip">The <see cref="RotateFlipType"/> to apply.</param>
        /// <returns>A rotated flipped gif based on the <see cref="RotateFlipType"/>; otherwise null.</returns>
        public static Bitmap RotateFlipGif(Bitmap bmp, RotateFlipType rotateFlip)
        {
            try
            {
                GifDecoder d = new GifDecoder(bmp);
                GifEncoder e = new GifEncoder(d.LoopCount);

                for (int i = 0; i < d.FrameCount; i++)
                {
                    using (GifFrame frame = d.GetFrame(i))
                    {
                        frame.Image.RotateFlip(rotateFlip);
                        e.EncodeFrame(frame);
                    }
                }
                return((Bitmap)e.Encode());
            }
            catch
            {
                return(null);
            }
        }
コード例 #3
0
        public static Bitmap DeepCloneGif(Image source, bool preserveMetaData = true)
        {
            GifDecoder decoder = new GifDecoder(source);
            GifEncoder encoder = new GifEncoder(decoder.LoopCount);

            for (int i = 0; i < decoder.FrameCount; i++)
            {
                using (GifFrame frame = decoder.GetFrame(i))
                {
                    encoder.EncodeFrame(frame);
                }
            }

            Image copy = encoder.Encode();

            if (preserveMetaData)
            {
                ImageHelper.CopyMetadata(source, copy);
            }

            return((Bitmap)copy);
        }
コード例 #4
0
        /// <summary>
        /// Invert the colors of every frame of a bitmap with animated frames.
        /// </summary>
        /// <param name="bmp"> The bitmap to invert. </param>
        /// <returns></returns>
        public static Bitmap InvertGif(Bitmap bmp)
        {
            try
            {
                GifDecoder d = new GifDecoder(bmp);
                GifEncoder e = new GifEncoder(d.LoopCount);

                for (int i = 0; i < d.FrameCount; i++)
                {
                    using (GifFrame frame = d.GetFrame(i))
                    {
                        InvertBitmapSafe(frame.Image);
                        e.EncodeFrame(frame);
                    }
                }
                return((Bitmap)e.Encode());
            }
            catch
            {
                return(null);
            }
        }