I am trying to take screenshot for every 10 miliseconds and set them as a Picturebox.image
with Timer. For few seconds program runs perfectly but after few seconds program is crashing. I tried to use Dispose()
Function in the end of the code to clear the memory but Dispose Function also gives an error. (increasing interval of timer doesn't worked)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace gameBot
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Bitmap screenshot;
Graphics GFX;
private void button1_Click(object sender, EventArgs e)
{
timer1.enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
takescreenshot();
}
private void takescreenshot()
{
screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
GFX = Graphics.FromImage(screenshot);
GFX.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size);
pictureBox1.Image = screenshot;
screenshot.Dispose();
}
}
}
The error is
"An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll
Additional information: Parameter is not valid."
Also program is using too much RAM before crashing (Maybe it's crashing because of out of memory exception?) As you can see in here