1

If i just take one screenshot without a timer the image screenshot of form1 will be fine:

This is the code when the timer is not enabled. The time is false.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Screenshots
{
    public partial class Form1 : Form
    {
        int countImages = 0;

        public Form1()
        {
            InitializeComponent();

            Cap();
            //this.Location = new Point(120, 50);
            //timer1.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Cap();
        }

        private void Cap()
        {
            using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width,                                            Screen.PrimaryScreen.Bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bmpScreenCapture))
                {
                    g.CopyFromScreen(this.Location.X,
                                   this.Location.Y,
                                   0, 0,
                                   this.Size,
                                   CopyPixelOperation.SourceCopy);
                }

                Rectangle section = new Rectangle(new Point(this.Location.X, this.Location.Y), new Size(this.Width, this.Height));
                Bitmap CroppedImage = CropImage(bmpScreenCapture, section);
                CroppedImage.Save("c:\\temp\\desktopscreens\\" + countImages + ".bmp", ImageFormat.Bmp);
            }
        }

        public Bitmap CropImage(Bitmap source, Rectangle section)
        {
            // An empty bitmap which will hold the cropped image
            Bitmap bmp = new Bitmap(section.Width, section.Height);

            Graphics g = Graphics.FromImage(bmp);

            // Draw the given area (section) of the source image
            // at location 0,0 on the empty bitmap (bmp)
            g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);

            return bmp;
        }
    }
}

The result is a screenshot of the form1 location in 0,0:

Form1 good screenshot

Now i'm using the timer. In the constructor i enable true the timer:

Now the constructor is like this:

public Form1()
        {
            InitializeComponent();

            timer1.Enabled = true;
            Cap();
            //this.Location = new Point(120, 50);
        }

The timer interval is set to 2000.

It will save now the same screenshot and will overwrite the other one every 2 seconds.

The result this time: And i didn't change anything but enabled true the timer.

With timer

Next test i added inside Cap() the line countimages++;

private void Cap()
        {
            using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width,                                            Screen.PrimaryScreen.Bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bmpScreenCapture))
                {
                    g.CopyFromScreen(this.Location.X,
                                   this.Location.Y,
                                   0, 0,
                                   this.Size,
                                   CopyPixelOperation.SourceCopy);
                }

                Rectangle section = new Rectangle(new Point(this.Location.X, this.Location.Y), new Size(this.Width, this.Height));
                Bitmap CroppedImage = CropImage(bmpScreenCapture, section);
                countImages++;
                CroppedImage.Save("c:\\temp\\desktopscreens\\" + countImages + ".bmp", ImageFormat.Bmp);
            }
        }

timer is enabled true.

The result is that i have now 5 images on the hard disk. The first one is fine saved a screenshot of form1 like in the first image i uploaded here.

But the other 4 images are with the black like in the second screenshot i added.

I can't figure out why when i enable true the timer1 to take screenshots of form1 every 2 seconds only the first screenshot is fine and the others have this black inside ?

What i want to do is to take a screenshot of form1 every 10ms or 100ms and when i move the form1 around i will have many screenshots of the form1 each time in another location.

But not sure why i'm getting the black color with the timer.

daniel llee
  • 67
  • 1
  • 8
  • 1
    Possible duplicate of [Screenshot method generates black images](https://stackoverflow.com/questions/24331011/screenshot-method-generates-black-images) – TomServo Jul 27 '17 at 20:05

0 Answers0