tags:

views:

549

answers:

4

This is a Windows application using C#. I want to capture a screen shot with a timer. The timer is set to a 5000 ms interval. As the timer is started, the desktop screen should be captured with the source window caption.

try
{
    System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
    timer.Tick += new EventHandler(timer2_Tick);
    timer.Interval = (100) * (50);
    timer.Enabled = true;
    timer.Start();

    ScreenShots sc = new ScreenShots();
    sc.pictureBox1.Image = system_serveillance.CaptureScreen.GetDesktopImage();

    while(sc.pictureBox1.Image != null)
    {
        sc.pictureBox1.Image.Save("s"+".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        sc.pictureBox1.Image = null;
    }

This code is not working properly. How can I make it work?

+1  A: 

You've started your timer, but your screen save routine doesn't appear to be in your timer tick code (unless you've omitted the code from the post. Similarly, you will be overwriting s.jpg everytime, and I assume this isn't what you want. The use of a while clause is also odd here because you only need to perform an if test.

Pete OHanlon
i know it is now working properly, can you tell me how can i do this?
ankush
sorry it is not working properly..i want to capture screen as timer's time is complete with capture time and window caption.
ankush
+4  A: 

The timer isn't firing because you're not handling the tick event. Pete has also pointed out your file will be overwritten on each tick.

It needs to look something more like the following. This isn't the exact code but it should give you an idea.

    private Int32 pictureCount = 0;

    public Form1()
    {
        timer1.Tick += new EventHandler(this.timer1_Tick);
        timer1.Interval = (100) * (50);
        timer1.Enabled = true;
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        /* Screen capture logic here */
        sc.pictureBox1.Image.Save(pictureCount.ToString() + ".jpg", ImageFormat.Jpeg);
        pictureCount++;
    }
Chalkey
A: 

Also you are declaring your Timer inside the try - so if you leave that scope your Timer will no longer exist. And as it stands, your code will get trapped in the while and your GUI will be able to do nothing else. (I think).

Benjol
A: 

I think the screen capture utility that comes with my open source BugTracker.NET app has the functionality you are looking for, or pretty close to it.

See http://ifdefined.com/blog/post/Screen-capture-utility-in-C-NET.aspx for what the screen capture utility looks like. The code for the delay looks like this, with the main window first hiding itself and then BeginInvoking itself to do the actual delay and capture. Download BugTracker.NET and you'll have the full source for the screen capture app.

void buttonCapture_Click(object sender, Exception e)
{
    this.Hide();
    BeginInvoke(new SimpleDelegate(CaptureForeground));
}

        private void CaptureForeground()
{
    // delay...
    System.Threading.Thread.Sleep(500 + (1000 * (int)numericUpDownDelay.Value));

    // Get foreground window rect using native calls
    IntPtr hWnd = GetForegroundWindow();
    RECT rct = new RECT();
    GetWindowRect(hWnd, ref rct);

    Rectangle r = new Rectangle();
    r.Location = new Point(rct.Left, rct.Top);
    r.Size = new Size(rct.Right - rct.Left, rct.Bottom - rct.Top);
    CaptureBitmap(r);

    this.Show();
}

private void CaptureBitmap(Rectangle r)
{
    bitmap = new Bitmap(r.Width, r.Height);
    {
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.CopyFromScreen(r.Location, new Point(0, 0), r.Size);
        }
    }
}
Corey Trager