I'm trying to take a screenshot every 50 ms and remove all colors that are not similar to a color that I've decided in the beginning, but the part of code that check the color of every pixel is too slow, so in the end it do a screenshot almost every 1.5s... Here is the code:
public int upX = 114;
public int upY = 28;
public Size size = new Size(1137,640);
public Color rosa = Color.FromArgb(255, 102, 153);
public int tollerance = 20;
private void button1_Click(object sender, EventArgs e){
button1.Enabled = false;
do{
Bitmap bmpScreenshot = screenshot();
deleteColors(bmpScreenshot, rosa);
picturebox1.image = bmpScreenshot;
Application.DoEvents();
} while (true);
}
public Bitmap screenshot() {
Bitmap bmpScreenshot = new Bitmap(size.Width, size.Height, PixelFormat.Format16bppRgb555);
Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen(upX, upY, 0, 0, size);
return bmpScreenshot;
}
public void deleteColors(Bitmap bitmap, Color colorToSave) {
for (int i = 0; i < bitmap.Width; i++){
for (int j = 0; j < bitmap.Height; j++){
Color c = bitmap.GetPixel(i, j);
if (!colorsAreSimilar(c, colorToSave, tollerance)){
bitmap.SetPixel(i, j, Color.White);
}
}
}
}
public bool colorsAreSimilar(Color a, Color b, int tollerance) {
if (Math.Abs(a.R - b.R) < tollerance && Math.Abs(a.G - b.G) < tollerance && Math.Abs(a.B - b.B) < tollerance) {
return true;
}
return false;
}
The screenshot part take 17 to 21ms, so it's already too high, but the delete part take 1300ms, so I'd like to fix that before, but... I don't know what i could do to make the code lighter.