/// <summary>
 ///     Add a new frame to the GIF async
 /// </summary>
 /// <param name="path">The image's path which will be added to the GIF stack</param>
 /// <param name="delay">The delay in milliseconds this GIF will be delayed (-1: Indicating class property delay)</param>
 /// <param name="quality">The GIFs quality</param>
 public async Task AddFrameAsync(string path, int delay = -1, GifQuality quality = GifQuality.Default, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var img = Helper.LoadImage(path))
     {
         await AddFrameAsync(img, delay, quality, cancellationToken);
     }
 }
示例#2
0
 /// <summary>
 ///     Add a new frame to the GIF
 /// </summary>
 /// <param name="path">The image's path which will be added to the GIF stack</param>
 /// <param name="delay">The delay in milliseconds this GIF will be delayed (-1: Indicating class property delay)</param>
 /// <param name="quality">The GIFs quality</param>
 public void AddFrame(string path, int delay = -1, GifQuality quality = GifQuality.Default)
 {
     using (var img = Helper.LoadImage(path))
     {
         AddFrame(img, delay, quality);
     }
 }
示例#3
0
        public static void SaveGif(this Image img, Stream stream, GifQuality quality)
        {
            if (quality == GifQuality.Default)
            {
                img.Save(stream, ImageFormat.Gif);
            }
            else
            {
                Quantizer quantizer;
                switch (quality)
                {
                case GifQuality.Grayscale:
                    quantizer = new GrayscaleQuantizer();
                    break;

                case GifQuality.Bit4:
                    quantizer = new OctreeQuantizer(15, 4);
                    break;

                case GifQuality.Default:
                case GifQuality.Bit8:
                default:
                    quantizer = new OctreeQuantizer(255, 4);
                    break;
                }

                using (var quantized = quantizer.Quantize(img))
                {
                    quantized.Save(stream, ImageFormat.Gif);
                }
            }
        }
示例#4
0
        public void LoadGifPicture(Image img, GifQuality quality)
        {
            List <byte> dataList;

            using (var ms = new MemoryStream()) {
                img.SaveGif(ms, quality);
                dataList = new List <byte>(ms.ToArray());
            }

            if (!AnalyzeGifSignature(dataList))
            {
                throw new Exception("File is not a gif!");
            }

            AnalyzeScreenDescriptor(dataList);

            var blockType = GetTypeOfNextBlock(dataList);

            while (blockType != GifBlockType.Trailer)
            {
                switch (blockType)
                {
                case GifBlockType.ImageDescriptor:
                    AnalyzeImageDescriptor(dataList);
                    break;

                case GifBlockType.Extension:
                    ThrowAwayExtensionBlock(dataList);
                    break;
                }

                blockType = GetTypeOfNextBlock(dataList);
            }
        }
示例#5
0
        /// <summary>
        ///     Add a new frame to the GIF
        /// </summary>
        /// <param name="image">The image to add to the GIF stack</param>
        /// <param name="delay">The delay in milliseconds this GIF will be delayed (-1: Indicating class property delay)</param>
        /// <param name="quality">The GIFs quality</param>
        public void AddFrame(Image image, int delay = -1, GifQuality quality = GifQuality.Default)
        {
            var gif = new GifClass();

            gif.LoadGifPicture(image, quality);

            if (_stream == null)
            {
                _stream = new FileStream(FilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
                _stream.Write(CreateHeaderBlock());
                _stream.Write(gif.ScreenDescriptor.ToArray());
                _stream.Write(CreateApplicationExtensionBlock(Repeat));
            }

            _stream.Write(CreateGraphicsControlExtensionBlock(delay > -1 ? delay : Delay));
            _stream.Write(gif.ImageDescriptor.ToArray());
            _stream.Write(gif.ColorTable.ToArray());
            _stream.Write(gif.ImageData.ToArray());

            FrameCount++;
        }
        /// <summary>
        ///     Add a new frame to the GIF
        /// </summary>
        /// <param name="image">The image to add to the GIF stack</param>
        /// <param name="delay">The delay in milliseconds this GIF will be delayed (-1: Indicating class property delay)</param>
        /// <param name="quality">The GIFs quality</param>
        public void AddFrame(Image image, int delay = -1, GifQuality quality = GifQuality.Default)
        {
            var gif = new GifClass();

            gif.LoadGifPicture(image, quality);

            if (!_createdHeader)
            {
                AppendToStream(CreateHeaderBlock());
                AppendToStream(gif.ScreenDescriptor.ToArray());
                AppendToStream(CreateApplicationExtensionBlock(Repeat));
                _createdHeader = true;
            }

            AppendToStream(CreateGraphicsControlExtensionBlock(delay > -1 ? delay : Delay));
            AppendToStream(gif.ImageDescriptor.ToArray());
            AppendToStream(gif.ColorTable.ToArray());
            AppendToStream(gif.ImageData.ToArray());

            FrameCount++;
        }
        /// <summary>
        ///     Add a new frame to the GIF async
        /// </summary>
        /// <param name="image">The image to add to the GIF stack</param>
        /// <param name="delay">The delay in milliseconds this GIF will be delayed (-1: Indicating class property delay)</param>
        /// <param name="quality">The GIFs quality</param>
        public async Task AddFrameAsync(Image image, int delay = -1, GifQuality quality = GifQuality.Default, CancellationToken cancellationToken = default(CancellationToken))
        {
            var gif = new GifClass();

            gif.LoadGifPicture(image, quality);

            if (!_createdHeader)
            {
                await AppendToStreamAsync(CreateHeaderBlock(), cancellationToken);
                await AppendToStreamAsync(gif.ScreenDescriptor.ToArray(), cancellationToken);
                await AppendToStreamAsync(CreateApplicationExtensionBlock(Repeat), cancellationToken);

                _createdHeader = true;
            }

            await AppendToStreamAsync(CreateGraphicsControlExtensionBlock(delay > -1 ? delay : Delay), cancellationToken);
            await AppendToStreamAsync(gif.ImageDescriptor.ToArray(), cancellationToken);
            await AppendToStreamAsync(gif.ColorTable.ToArray(), cancellationToken);
            await AppendToStreamAsync(gif.ImageData.ToArray(), cancellationToken);

            FrameCount++;
        }
示例#8
0
 public void AddFrame(string path, GifQuality quality = GifQuality.Default)
 {
     using (Image img = Helper.LoadImage(path)) {
         AddFrame(img, quality);
     }
 }
示例#9
0
    public static void SaveToGif(List <MemoryStream> lms)
    {
        SaveFileDialog saveFileDialog = new SaveFileDialog();

        saveFileDialog.Filter = "Gif Image|*.gif";
        saveFileDialog.Title  = "Save a Gif File";

        GifQuality GQ = new GifQuality();

        switch (App.GV.GifQuality)
        {
        case "Default":
            GQ = GifQuality.Default;
            break;

        case "Bit8":
            GQ = GifQuality.Bit8;
            break;

        case "Bit4":
            GQ = GifQuality.Bit4;
            break;

        case "Grayscale":
            GQ = GifQuality.Grayscale;
            break;

        default:
            GQ = GifQuality.Default;
            break;
        }

        string fileName = GetFileNameNoEx(App.GV.SelectFile);

        if (App.GV.SelectAnimeName != "")
        {
            fileName += $"_{App.GV.SelectAnimeName}";
        }
        if (App.GV.SelectSkin != "")
        {
            fileName += $"_{App.GV.SelectSkin}";
        }

        saveFileDialog.FileName = fileName;
        saveFileDialog.ShowDialog();

        if (saveFileDialog.FileName != "")
        {
            using (AnimatedGifCreator gifCreator = AnimatedGif.AnimatedGif.Create(saveFileDialog.FileName, 1000 / App.GV.Speed))
            {
                foreach (MemoryStream img in lms)
                {
                    using (img)
                    {
                        gifCreator.AddFrame(Image.FromStream(img), GQ);
                        img.Dispose();
                    }
                }
            }
        }
        else
        {
            foreach (MemoryStream ms in lms)
            {
                ms.Dispose();
            }
        }
        lms.Clear();
    }
示例#10
0
    public static void SaveToGif(List <MemoryStream> lms, float time = 0)
    {
        SaveFileDialog saveFileDialog = new SaveFileDialog();

        saveFileDialog.Filter = "Gif Image|*.gif";
        saveFileDialog.Title  = "Save a Gif File";

        GifQuality GQ = new GifQuality();

        switch (App.GV.GifQuality)
        {
        case "Default":
            GQ = GifQuality.Default;
            break;

        case "Bit8":
            GQ = GifQuality.Bit8;
            break;

        case "Bit4":
            GQ = GifQuality.Bit4;
            break;

        case "Grayscale":
            GQ = GifQuality.Grayscale;
            break;

        default:
            GQ = GifQuality.Default;
            break;
        }
        string fileName = GetFileNameNoEx(App.GV.SelectFile);

        if (App.GV.SelectAnimeName != "")
        {
            fileName += $"_{App.GV.SelectAnimeName}";
        }
        if (App.GV.SelectSkin != "")
        {
            fileName += $"_{App.GV.SelectSkin}";
        }

        saveFileDialog.FileName = fileName;
        saveFileDialog.ShowDialog();
        int delay = 0;

        if (time == 0)
        {
            delay = 1000 / App.GV.Speed;
        }
        else
        {
            delay = (int)(time * 1000 / lms.Count);
        }

        if (saveFileDialog.FileName != "")
        {
            using (AnimatedGifCreator gifCreator = AnimatedGif.AnimatedGif.Create(saveFileDialog.FileName, delay, App.GV.IsLoop == true ? 0 : 1))
            {
                foreach (MemoryStream msimg in lms)
                {
                    using (msimg)
                    {
                        using (Image img = Image.FromStream(msimg))
                        {
                            gifCreator.AddFrame(img, -1, GQ);
                        }
                    }
                }
            }
        }
        else
        {
            foreach (MemoryStream ms in lms)
            {
                ms.Dispose();
            }
        }
        lms.Clear();
        GC.Collect();
    }
示例#11
0
    public static void SaveToGif2(float time = 0)
    {
        SaveFileDialog saveFileDialog = new SaveFileDialog();

        saveFileDialog.Filter = "Gif Image|*.gif";
        saveFileDialog.Title  = "Save a Gif File";

        GifQuality gifQuality = new GifQuality();

        switch (App.globalValues.GifQuality)
        {
        case "Default":
            gifQuality = GifQuality.Default;
            break;

        case "Bit8":
            gifQuality = GifQuality.Bit8;
            break;

        case "Bit4":
            gifQuality = GifQuality.Bit4;
            break;

        case "Grayscale":
            gifQuality = GifQuality.Grayscale;
            break;

        default:
            gifQuality = GifQuality.Default;
            break;
        }
        string fileName = GetFileNameNoEx(App.globalValues.SelectAtlasFile);

        if (App.globalValues.SelectAnimeName != "")
        {
            fileName += $"_{App.globalValues.SelectAnimeName}";
        }
        if (App.globalValues.SelectSkin != "")
        {
            fileName += $"_{App.globalValues.SelectSkin}";
        }

        saveFileDialog.FileName = fileName;
        saveFileDialog.ShowDialog();

        string[] pngList = Directory.GetFiles($"{App.rootDir}\\Temp\\", "*.png", SearchOption.TopDirectoryOnly);


        int delay = 0;

        if (time == 0)
        {
            delay = 1000 / App.globalValues.Speed;
        }
        else
        {
            delay = (int)(time * 1000 * (App.globalValues.Speed / 30f) / pngList.Length);
        }

        if (saveFileDialog.FileName != "")
        {
            using (AnimatedGifCreator gifCreator = AnimatedGif.AnimatedGif.Create(saveFileDialog.FileName, delay, App.globalValues.IsLoop == true ? 0 : 1))
            {
                foreach (string path in pngList)
                {
                    using (FileStream fsimg = new FileStream(path, FileMode.Open))
                    {
                        using (Image img = Image.FromStream(fsimg))
                        {
                            gifCreator.AddFrame(img, -1, gifQuality);
                        }
                    }
                }
            }
        }
        else
        {
        }
        ClearCacheFile();

        GC.Collect();
    }