Advertisement
benshepherd

Capture screenshot save as GIF

Jul 15th, 2013
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Windows.Forms;
  9. using NGif;
  10.  
  11. namespace GifRec
  12. {
  13.     class GifRecorder
  14.     {
  15.         List<Bitmap> frames = new List<Bitmap>();
  16.         Timer timer;
  17.         Rectangle region;
  18.         int iframe = 0;
  19.         int duration = 5;
  20.         int fps = 8;
  21.         int ms = 0;
  22.         string path;
  23.         Action<string> SetText;
  24.         Action<bool> ToggleStart;
  25.  
  26.         public GifRecorder(Rectangle _region, int _duration, Action<string> _SetText, Action<bool> _ToggleStart)
  27.         {
  28.             region = _region;
  29.             duration = _duration;
  30.             SetText = _SetText;
  31.             ToggleStart = _ToggleStart;
  32.  
  33.             path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\GifRec\\";
  34.  
  35.             if (!Directory.Exists(path))
  36.                 Directory.CreateDirectory(path);
  37.  
  38.             path += RandomString.Create(5) + ".gif";
  39.         }
  40.  
  41.         public void Start()
  42.         {
  43.             frames.Clear();
  44.  
  45.             timer = new Timer();
  46.             timer.Tick += new EventHandler(timer_Tick);
  47.             timer.Interval = 1000 / fps;
  48.             timer.Enabled = true;
  49.         }
  50.  
  51.         private void timer_Tick(object sender, EventArgs e)
  52.         {
  53.             if (ms / 1000 == duration)
  54.             {
  55.                 timer.Enabled = false;
  56.  
  57.                 SetText("Creating GIF");
  58.  
  59.                 System.Threading.Thread genGifThread = new System.Threading.Thread(GenerateGif);
  60.                 genGifThread.Start();
  61.  
  62.                 return;
  63.             }
  64.  
  65.             frames.Add(ScreenCap.Crop(ScreenCap.Fullscreen(), region));
  66.  
  67.             SetText("Elapsed time " + (ms / 1000 + 1));
  68.  
  69.             ms += 1000 / fps;
  70.         }
  71.  
  72.         private void GenerateGif()
  73.         {
  74.             AnimatedGifEncoder e = new AnimatedGifEncoder();
  75.             e.Start(path);
  76.             e.SetDelay(1000 / fps);
  77.             e.SetQuality(50);
  78.  
  79.             for (int i = 0; i < frames.Count; i++)
  80.             {
  81.                 e.AddFrame((Image)frames[i]);
  82.             }
  83.  
  84.             e.Finish();
  85.  
  86.             SetText("Done");
  87.             Taskbar.Balloon(path, "GIF Saved");
  88.             ToggleStart(true);
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement