0

I am using a CImage class to save screenshots of the desktop. Since the desktop is in 1920x1080 the images are also in 1920x1080. However, since i want to do some work on those images and their size makes it very computationally expensive and slows the whole process. i wanted to know how to resize the images before they are saved to a file. I used the stretchblt() function, although i get the correct size of image the image itself is all black. Any help will be greatly appreciated. The code is below:

void GDIInitScreenCapture()
{
    _screen_capture_worker.nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
    _screen_capture_worker.nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
    _screen_capture_worker.hDesktopWnd = GetDesktopWindow();
    _screen_capture_worker.hDesktopDC = GetDC(_screen_capture_worker.hDesktopWnd);
    _screen_capture_worker.hCaptureDC = CreateCompatibleDC(_screen_capture_worker.hDesktopDC);

    _screen_capture_worker.hCaptureBitmap = CreateCompatibleBitmap(_screen_capture_worker.hDesktopDC,
    _screen_capture_worker.nScreenWidth, _screen_capture_worker.nScreenHeight);

    SelectObject(_screen_capture_worker.hCaptureDC, _screen_capture_worker.hCaptureBitmap);
}

void GDIReleaseScreenCapture()
{
    ReleaseDC(_screen_capture_worker.hDesktopWnd, _screen_capture_worker.hDesktopDC);
    DeleteDC(_screen_capture_worker.hCaptureDC);
    DeleteObject(_screen_capture_worker.hCaptureBitmap);
}

bool GDITakeScreenshots(std::string file_name)
{
    BitBlt(_screen_capture_worker.hCaptureDC, 0, 0, 
    _screen_capture_worker.nScreenWidth, _screen_capture_worker.nScreenHeight,
    _screen_capture_worker.hDesktopDC, 0, 0, SRCCOPY | CAPTUREBLT);
    CImage image;
    CImage simage;
    simage.Create(640, 480, 24);
    image.Attach(_screen_capture_worker.hCaptureBitmap);
    SetStretchBltMode(simage.GetDC(), HALFTONE);
    image.StretchBlt(simage.GetDC(), 0, 0, 640, 480, SRCCOPY);
    simage.Save(file_name.c_str(), Gdiplus::ImageFormatJPEG);
    simage.ReleaseDC();

    return true;
}
Abrar3616
  • 9
  • 1
  • I would like to clarify that i also used ColorOnColor mode and the result was same, i.e. black image of correct required size – Abrar3616 Jun 25 '21 at 11:27
  • As far as I'm concerned, Once `DeleteObject` destroys the img data, the returned img may not work properly. I suggest you could try to save the img before DeleteObject. – Jeaninez - MSFT Jun 28 '21 at 06:21
  • Yeah, i get that but these are just the function definitions I call the delete function only when i want to stop the screen shot process. – Abrar3616 Jun 28 '21 at 06:54

0 Answers0