WindowsDesktopDuplicationSample
WindowsDesktopDuplicationSample copied to clipboard
Tearing while 'windup' window moves
Not really an issue. More like FYI.
While the 'windup' window is moved with the mouse I get a lot of tearing. The painted image cuts off and is just white.
I've removed the WM_PAINT message handling and instead try to paint 'instantly'. That solved the tearing issue. Except if the window is moved into the screen from outside there is still a little bit - but that makes sense because it cannot capture from outside the screens size ahead of time.
I have no clue whether this is a proper fix. Nice demo project Ben :) Much faster capture than GDI and definitely better and easier than Microsoft docs.
case WM_TIMER:
//if (dup.CaptureNext())
//InvalidateRect(hWnd, nullptr, false);
if (dup.CaptureNext())
{
InvalidateRect(hWnd, nullptr, false);
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
BITMAPINFO inf;
memset(&inf, 0, sizeof(inf));
inf.bmiHeader.biSize = sizeof(inf.bmiHeader);
inf.bmiHeader.biWidth = dup.Latest.Width;
inf.bmiHeader.biHeight = -dup.Latest.Height;
inf.bmiHeader.biPlanes = 1;
inf.bmiHeader.biBitCount = 32;
inf.bmiHeader.biCompression = BI_RGB;
void* bits = nullptr;
HDC srcDC = CreateCompatibleDC(hdc);
HBITMAP dib = CreateDIBSection(hdc, &inf, 0, &bits, nullptr, 0);
memcpy(bits, dup.Latest.Buf.data(), dup.Latest.Width * dup.Latest.Height * 4);
SelectObject(srcDC, dib);
BitBlt(hdc, 0, 0, dup.Latest.Width, dup.Latest.Height, srcDC, 0, 0, SRCCOPY);
DeleteObject(dib);
DeleteObject(srcDC);
EndPaint(hWnd, &ps);
}
break;
/*
case WM_PAINT:{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
BITMAPINFO inf;
memset(&inf, 0, sizeof(inf));
inf.bmiHeader.biSize = sizeof(inf.bmiHeader);
inf.bmiHeader.biWidth = dup.Latest.Width;
inf.bmiHeader.biHeight = -dup.Latest.Height;
inf.bmiHeader.biPlanes = 1;
inf.bmiHeader.biBitCount = 32;
inf.bmiHeader.biCompression = BI_RGB;
void* bits = nullptr;
HDC srcDC = CreateCompatibleDC(hdc);
HBITMAP dib = CreateDIBSection(hdc, &inf, 0, &bits, nullptr, 0);
memcpy(bits, dup.Latest.Buf.data(), dup.Latest.Width * dup.Latest.Height * 4);
SelectObject(srcDC, dib);
BitBlt(hdc, 0, 0, dup.Latest.Width, dup.Latest.Height, srcDC, 0, 0, SRCCOPY);
DeleteObject(dib);
DeleteObject(srcDC);
EndPaint(hWnd, &ps);
} break;
*/