1

already searched but did not get help from anywhere! Capture a sceenshot and save it in a folder

Community
  • 1
  • 1
  • 6
    Possible duplicate of [Capture screenshot of active window?](http://stackoverflow.com/questions/1163761/capture-screenshot-of-active-window) – Zein Makki Jul 14 '16 at 06:12
  • take screen shot is done...but problem is that i want to save image after every 5 min...using this method var image = ScreenCapture.CaptureActiveWindow(); image.Save(@"D:\documents\FYP\snippetsource.jpg", ImageFormat.Jpeg);.........but it replaces old image i want to save all images – Muneeb Ur Rehman Jul 14 '16 at 06:42

3 Answers3

1

So your problem is in how to run a task every 5 minutes. Use Timers:

var timer = new System.Timers.Timer(TimeSpan.FromMinutes(5).TotalMilliseconds);

timer.Elapsed += Timer_Elapsed;
timer.Start();

private static void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
    // call the function that takes the screenshot
}
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • timer.Elapsed += Timer_Elapsed;......... it does not call to Timer_Elapsed(object sender, ElapsedEventArgs e)..........I'm calling it like this: var timer = new System.Timers.Timer(TimeSpan.FromSeconds(2).TotalMilliseconds); timer.Elapsed += Timer_Elapsed; – Muneeb Ur Rehman Jul 14 '16 at 09:08
  • @muneeb sorry you need to use `timer.Start();` – Zein Makki Jul 14 '16 at 09:09
  • Thanks its done.......var aTimer = new System.Timers.Timer(1000); aTimer.Elapsed += new ElapsedEventHandler(Timer_Elapsed); aTimer.Interval = 1000; aTimer.Enabled = true;......... this code solved my problem.....really thanks to you also :) – Muneeb Ur Rehman Jul 14 '16 at 09:21
  • @muneeb http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Zein Makki Jul 14 '16 at 09:46
0

use this function to capture screen:

private static Image CaptureScreen()
    {
        Rectangle screenSize = Screen.PrimaryScreen.Bounds;
        var target = new Bitmap(screenSize.Width, screenSize.Height);
        using (Graphics g = Graphics.FromImage(target))
        {
            g.CopyFromScreen(0, 0, 0, 0, new Size(screenSize.Width, screenSize.Height));
        }
        return target;
    }

Saving to file:

var currentImage = CaptureScreen();
currentImage.Save(Path.Combine("Your directory path", "Your file name"));
Leonid Malyshev
  • 475
  • 1
  • 6
  • 14
0

Are you creating a new file name each time you save the image, meaning, instead of:

image.Save(@"D:\documents\FYP\snippetsource.jpg", ImageFormat.Jpeg);

Do something like:

image.Save(string.Format(@"D:\documents\FYP\snippetsource_{0}.jpg", Guid.NewGuid()), ImageFormat.Jpeg);

You can change Guid.NewCuid() with whatever unique string that makes more sense to your application.

Hope it helps!

Itay Podhajcer
  • 2,616
  • 2
  • 9
  • 14
  • Thanks its helpful.......var currentImage = ScreenCapture.CaptureScreen(); //currentImage.Save(Path.Combine(@"D:\documents\FYP", "abc.jpg")); currentImage.Save(string.Format(@"dll\snippetsource_{0}.jpg", Guid.NewGuid()), ImageFormat.Jpeg);............when I'm calling Time_Elapsed it does not work............I'm calling it like this: private void timer1_Tick(object sender, EventArgs e) { var timer = new System.Timers.Timer(TimeSpan.FromSeconds(2).TotalMilliseconds); timer.Elapsed += Timer_Elapsed; } – Muneeb Ur Rehman Jul 14 '16 at 07:28
  • Try storing the created timer instance in a class level private memeber instead of a local method variable. – Itay Podhajcer Jul 14 '16 at 07:34