capture a specified window and use filter on it with OpenCV

Viewed 25

I want to capture a window given from handle. Already did it but in a while loop it takes so much memory even when I made a Thread.Sleep(200); Did you have a better solution for that? I want to capture a window and in real-time I want to change the HSV Filter via OpenCV.

That is my class what I use to capture a window

internal class ScreenCapturer
{
[StructLayout(LayoutKind.Sequential)]
        private struct Rect
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);

        public static Bitmap CaptureWindow(IntPtr handle)
        {
            var rect = new Rect();
            GetWindowRect(handle, ref rect);

            var bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left - 9, rect.Bottom - rect.Top - 9);
            var result = new Bitmap(bounds.Width, bounds.Height);

            var graphics = Graphics.FromImage(result);
            graphics.CopyFromScreen(bounds.Left, bounds.Top, -9, -31, bounds.Size);
            graphics.Dispose();

            return result;
        }
}

And here is my While loop:

private void btn1_click(sender, e)
{
    Thread t1 = new Thread(startCapturing);
    t1.Start();
}

private void startCapturing()
{
    while(true)
        {
            picBox1.Image = ScreenCapturer.CaptureWindow(p.MainWindowHandle);
        }
}
0 Answers
Related