public static void PngToGIF(string png, string info, string gif)
        {
            var reader = new BinaryReader(new FileStream(info, FileMode.Open));

            reader.ReadInt16();              // width
            reader.ReadInt16();              // height
            int rows  = reader.ReadInt16();
            int lines = reader.ReadInt16();

            if (rows == 1 & lines == 1)
            {
                return;
            }
            int rect_width  = reader.ReadInt16();
            int rect_height = reader.ReadInt16();

            reader.Close();

            var gif_creator = new AnimatedGifCreator(gif, 115, 0);
            var source      = new Bitmap(png);

            for (int i = 0; i < lines; i++)
            {
                for (int j = 0; j < rows; j++)
                {
                    var frame_rect = new Rectangle(j * rect_width, i * rect_height, rect_width, rect_height);
                    var frame      = CropImage(source, frame_rect);
                    gif_creator.AddFrame(frame);
                }
            }
            gif_creator.Dispose();
        }
Ejemplo n.º 2
0
        private void MakeGIF(string saveTo, Action <int, int> callback)
        {
            var files = Directory.GetFiles(ScreenRecordOption.CachePath);

            // 保存每个文件流,以方便在后面关闭文件
            var temp = new List <FileStream>(files.Length);

            int qualityValue = 3;

            this.InvokeMethod(() =>
            {
                qualityValue = tbQuality.Value;
            });
            GifQuality quality;

            switch (qualityValue)
            {
            case 1:
                quality = GifQuality.Grayscale;
                break;

            case 2:
                quality = GifQuality.Bit4;
                break;

            case 3:
                quality = GifQuality.Bit8;
                break;

            default:
                quality = GifQuality.Default;
                break;
            }
            var gifStream = new FileStream(saveTo, FileMode.Create);
            var gif       = new AnimatedGifCreator(gifStream,
                                                   1000 / ScreenRecordOption.Fps, ScreenRecordOption.RepeatCount);

            for (int i = 0; i < files.Length; i++)
            {
                callback(i, files.Length);
                var stream = LoadImage(files[i]);
                temp.Add(stream);
                gif.AddFrame(Image.FromStream(stream), quality: quality);
            }
            callback(files.Length, files.Length);
            gif.Dispose();

            foreach (var item in temp)
            {
                item.Close();
                item.Dispose();
            }
            temp.Clear();

            GC.Collect();
        }
Ejemplo n.º 3
0
 public override void Finish()
 {
     gif.Dispose();
 }