I'm trying to copy parts of the screen, modify them, and then copy those parts back to the screen. This is in windows, using C++.
The general structure of my code looks like this:
HDC hdcDesktop = GetDC(NULL);
HDC hdcTemp = CreateCompatibleDC(hdcDesktop);
BitBlt(hdcTemp, 0, 0, 100, 100, hdcDesktop, 100, 100, SRCCOPY);
BitBlt(hdcDesktop, rand() % 1920, rand() % 1080, 100, 100, hdcTemp, 0, 0, SRCCOPY);
This should copy a 100x100 portion of the screen starting at (100, 100) to some random part of the screen. This doesn't work, however. What am I doing wrong?
There are a few issues with this code:
As indicated by the docs,
CreateCompatibleDC
creates a new in-memory image that is 1x1 pixels. This is obviously not big enough for your 100x100 chunk of image. You should probably useCreateCompatibleBitmap
.The coordinates passed to BitBlt are:
in that order. You seem to be confusing nXSrc/nYSrc with nWidth/nHeight. Check your numbers.
Wanton abuse of the desktop surface like this may actually (1) be disallowed and (2) produce unexpected results. Be careful what you are attempting to achieve.