void Camera_OnNewFrame() { if (_isClosing) { return; } try { Bitmap image = _cameraControl.Camera.GetCurrentBitmap; var found = GetDetectedImage(image); if (found == UCProgramItem.ProgramItemEnum.NA) { } else if (_lastDetectedItem != found && found == UCProgramItem.ProgramItemEnum.Start) { say("Running your program"); _currentRunningIndex = 0; flowLayoutPanel1.Invoke(new Action(() => { var item = (UCProgramItem)flowLayoutPanel1.Controls[_currentRunningIndex]; item.BackColor = Color.Green; _executor.StartScriptASync(item.GetCode()); })); } else if (_lastDetectedItem != found) { flowLayoutPanel1.Invoke(new Action(() => Invokers.AddControl(flowLayoutPanel1, new UCProgramItem(found)))); say(string.Format("Added {0} command", found)); } _lastDetectedItem = found; // Rather than creating a new bitmap for the picturebox, we simply overwrite the bitmap data with the bmTmp data // This way, when the bmpTmp is destroyed from the using, the picturebox has the brand new image data pictureBox1.Invoke(new Action(() => EZ_B.Camera.CopyBitmapMemory(image, (Bitmap)pictureBox1.Image))); Invokers.SetRefresh(pictureBox1); } catch (Exception ex) { Invokers.SetAppendText(tbLog, true, ex.ToString()); Invokers.SetEnabled(btnAttachCamera, true); } }