2

I've seen this tutorial on how to capture the screen with cursor. Now I added a timer and datagridview and I want to save every capture in the datagridview. Here is what I did:

private void Display(Bitmap desktop)
{
    Graphics g;
    Rectangle r;
    if (desktop != null)
    {
        r = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height);
        g = pictureBox1.CreateGraphics();
        g.DrawImage(desktop, r);
        g.Flush();

            Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, g);

        dataGridView1.Rows.Add(bmp);
    }
}

But all I get is white images like this:

enter image description here

I can't reach the point where I can save what appear on picturebox and add it to the datagridview

Maged E William
  • 436
  • 2
  • 9
  • 27

1 Answers1

2

Using CreateGraphics is a temporary drawing on the screen, so the image isn't getting transferred to the bitmap.

Try drawing directly instead:

private void Display(Bitmap desktop) {
  if (desktop != null) {
    Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, 
                            pictureBox1.ClientSize.Height);
    using (Graphics g = Graphics.FromImage(bmp)) {
      g.DrawImage(desktop, Point.Empty);
    }
    pictureBox1.Image = bmp;
    dataGridView1.Rows.Add(bmp);
  }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225