Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Windows.Forms;
- using NGif;
- namespace GifRec
- {
- class GifRecorder
- {
- List<Bitmap> frames = new List<Bitmap>();
- Timer timer;
- Rectangle region;
- int iframe = 0;
- int duration = 5;
- int fps = 8;
- int ms = 0;
- string path;
- Action<string> SetText;
- Action<bool> ToggleStart;
- public GifRecorder(Rectangle _region, int _duration, Action<string> _SetText, Action<bool> _ToggleStart)
- {
- region = _region;
- duration = _duration;
- SetText = _SetText;
- ToggleStart = _ToggleStart;
- path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\GifRec\\";
- if (!Directory.Exists(path))
- Directory.CreateDirectory(path);
- path += RandomString.Create(5) + ".gif";
- }
- public void Start()
- {
- frames.Clear();
- timer = new Timer();
- timer.Tick += new EventHandler(timer_Tick);
- timer.Interval = 1000 / fps;
- timer.Enabled = true;
- }
- private void timer_Tick(object sender, EventArgs e)
- {
- if (ms / 1000 == duration)
- {
- timer.Enabled = false;
- SetText("Creating GIF");
- System.Threading.Thread genGifThread = new System.Threading.Thread(GenerateGif);
- genGifThread.Start();
- return;
- }
- frames.Add(ScreenCap.Crop(ScreenCap.Fullscreen(), region));
- SetText("Elapsed time " + (ms / 1000 + 1));
- ms += 1000 / fps;
- }
- private void GenerateGif()
- {
- AnimatedGifEncoder e = new AnimatedGifEncoder();
- e.Start(path);
- e.SetDelay(1000 / fps);
- e.SetQuality(50);
- for (int i = 0; i < frames.Count; i++)
- {
- e.AddFrame((Image)frames[i]);
- }
- e.Finish();
- SetText("Done");
- Taskbar.Balloon(path, "GIF Saved");
- ToggleStart(true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement