1

I used this dependable method to take a screenshot. I call it dependable because it worked for me in all versions of Windows since XP. However in Win10, it now gives me transparent section. Some areas in some types of windows are just transparent, I can't explain it.

This a screenshot with normal print screen key on keyboard -

screenshot

This is screenshot of the same section using my code below -

screenshot

It seems that windows of file type "File Explorer" have certain areas gone transparent. The above screenshots are of Notepad++ though.

Here is my algorithm:

#include <Windows.h>
#include <stdio.h>

int WINAPI WinMain(HINSTANCE hinst, HINSTANCE, LPSTR, int)
{
    MessageBox(0, L"Hello World", L"Unipen", MB_ICONINFORMATION);


    int i = 0;
    DISPLAY_DEVICE device;

    device.cb = sizeof(device);


    while (EnumDisplayDevices(NULL, i, &device, 0) && ++i) {
        if ((device.StateFlags & DISPLAY_DEVICE_ACTIVE) != DISPLAY_DEVICE_ACTIVE) {
            MessageBox(0, device.DeviceName, L"CONTINUE", MB_ICONINFORMATION);
            continue;
        }
        MessageBox(0, device.DeviceName, L"BREAK", MB_ICONINFORMATION);
        break;
    }

    size_t screenWidth = 1920;
    size_t screenHeight = 1200;
    size_t colorLen = 4;

    HDC hdcScreen;
    hdcScreen = CreateDC(NULL, device.DeviceName, NULL, NULL);
    if (hdcScreen == (HDC)NULL) {
        MessageBox(0, L"UnableToCreateDC", L"ERROR", MB_ICONINFORMATION);
        return 0;
    }

    HDC hdcMemoryDC;
    hdcMemoryDC = CreateCompatibleDC(hdcScreen);
    if (hdcMemoryDC == (HDC)NULL) {
        DeleteDC(hdcScreen);
        MessageBox(0, L"UnableToCreateCompatibleDC", L"ERROR", MB_ICONINFORMATION);
        return 0;
    }

    BITMAPINFO bmi;
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = (LONG)screenWidth;
    bmi.bmiHeader.biHeight = (-1)*(LONG)screenHeight;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = BI_RGB;

    BYTE *pixelBuffer;
    HBITMAP hbmp;
    hbmp = CreateDIBSection(hdcScreen, &bmi, DIB_RGB_COLORS, (void **)&pixelBuffer, NULL, 0);
    if (hbmp == (HBITMAP)NULL) {
        DeleteDC(hdcScreen);
        DeleteDC(hdcMemoryDC);
        MessageBox(0, L"UnableToCreateDIBSection", L"ERROR", MB_ICONINFORMATION);
        return 0;
    }

    //HGDIOBJ rez_selected;
    HBITMAP rez_selected_bmp;
    rez_selected_bmp = (HBITMAP)SelectObject(hdcMemoryDC, hbmp);
    if (rez_selected_bmp == (HBITMAP)NULL) {
        DeleteDC(hdcScreen);
        DeleteDC(hdcMemoryDC);
        DeleteObject(hbmp);
        MessageBox(0, L"UnableToCreateDIBSection", L"ERROR", MB_ICONINFORMATION);
        return 0;
    }

    BitBlt(hdcMemoryDC, 0, 0, screenWidth, screenHeight, hdcScreen, 0, 0, SRCCOPY);


    //(void) SelectObject(hdcMemoryDC, rez_selected_bmp);

    if (fopen_s(&stream, "C:\\Users\Vayeate\\Desktop\\blah.txt", "w") == 0) {
        for(size_t px = 0 ; px < (screenHeight * screenWidth * 4) ; ++px) {
            fprintf(stream, "%hhu, ", pixelBuffer[px]);
        }
        fclose(stream);
    }

    DeleteDC(hdcScreen);
    DeleteDC(hdcMemoryDC);
    DeleteObject(hbmp);

    MessageBox(0, L"DONE", L"Unipen", MB_ICONINFORMATION);

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Noitidart
  • 35,443
  • 37
  • 154
  • 323

0 Answers0