[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Screen capture CreateDIBSection doesn't capture LayeredWindow

Posted on 2010-09-20
9
Medium Priority
?
2,274 Views
Last Modified: 2013-12-03
Hello, I am trying to capture a screen shot using this code:

...
HWND hwnd=0;
hdc = GetWindowDC(hwnd);
memdc = CreateCompatibleDC(hdc);
...
hbmp = CreateDIBSection(hdc, &bmpinfo, DIB_PAL_COLORS, &pBits, NULL, 0)
SelectObject(memdc, hbmp);
...

Open in new window


But I layered windows doesn't appear inside the shot, it is like them were invisible, only the desktop and other windows are captured.

Using "print screen" I get a full screen shot including layered window, then how can I correct that ?

Thanks
0
Comment
Question by:Greg2FS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33715237
The code you posted does not contain BitBlt call or any other operation that will copy the pixels. It is also not clear from where to where you copy.
CreateDIBSection creates a DIB section. It does not capture any screen. Mainly, it is a way to create HBITMAP. pBits - this is the array of the pixels.
Sorry if I simplified too much.
In this EE article "Win32: Capture an image" (http://www.experts-exchange.com/A_1739.html) you can find the code that works. There is an example that takes screenshots from the desktop.
About the layered window (if your need):
Win32: Semi-Transparent Window: http://www.experts-exchange.com/A_1783.html 
 (There is the screenshot as an evidence for you).
0
 
LVL 1

Author Comment

by:Greg2FS
ID: 33715824
Sorry I didn't know exactly what does the job of taking the screenshot, but my code was longer and it seems that the code you link does exactly the same: the screenshot is taken but my layered window is not captured...
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33716098
No problem.
If you need the screenshots, you need to take the code from the article:
Win32: Capture an image
http://www.experts-exchange.com/A_1739.html 
 
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:Greg2FS
ID: 33716244
I tried this code and said it does the same thing as mine.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33716358
Yes. You are right. GetDC from the desktp does not help for the window with the WS_EX_LAYERED.
For a test, I took the screenshot of the layered window itself.

#include <Windows.h>

BOOL Capture(HDC, LPRECT, LPCWSTR);

int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                      LPWSTR lpCmdLine, int nCmdShow)
{
    HWND hWnd;
    HDC hDC;
    RECT rect;
    hWnd = FindWindow(NULL, L"Semi-Transparent Window");
    SetForegroundWindow(hWnd);
    ::GetWindowRect(hWnd, &rect);
    ScreenToClient(hWnd, (LPPOINT)&rect);
    ScreenToClient(hWnd, (LPPOINT)&rect.right);
    hDC = GetDC(hWnd);

    Capture(hDC, &rect, L"capture.bmp");
    return 0;
}

BOOL Capture(HDC hDC, LPRECT lpRect, LPCWSTR lpszFile)
{
    BITMAPINFO bmi = { 0 };

    LPBYTE pBits = NULL;
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = lpRect->right - lpRect->left;
    bmi.bmiHeader.biHeight = lpRect->bottom - lpRect->top;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = BI_RGB;
    HBITMAP hBitmap = CreateDIBSection(hDC, &bmi, 
        DIB_RGB_COLORS, (LPVOID*)&pBits, NULL, 0);

    HDC hMemDC = CreateCompatibleDC(hDC);
    HGDIOBJ hOld = SelectObject(hMemDC, hBitmap);
    BitBlt(hMemDC, 0, 0, 
        lpRect->right - lpRect->left,
        lpRect->bottom - lpRect->top,
        hDC, lpRect->left, lpRect->top, SRCCOPY);
    SelectObject(hMemDC, hOld);
    DeleteDC(hMemDC);

    DWORD nImageSize = ((((bmi.bmiHeader.biWidth * 
        bmi.bmiHeader.biBitCount) 
        + 31) & ~31) >> 3) 
        * bmi.bmiHeader.biHeight;

    BITMAPFILEHEADER header = { 0 };
    header.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + 
        (DWORD)sizeof(BITMAPINFOHEADER); 
    header.bfSize = nImageSize + sizeof(BITMAPFILEHEADER) + 
        sizeof(BITMAPINFOHEADER);
    header.bfType = 0x4D42;

    HANDLE hFile = CreateFile(lpszFile, GENERIC_WRITE,
        0, NULL, CREATE_ALWAYS, 
        FILE_ATTRIBUTE_NORMAL, NULL);   

    if (hFile != INVALID_HANDLE_VALUE) 
    {
        DWORD nWritten = 0;;
        WriteFile(hFile, (LPVOID)&header, 
            sizeof(BITMAPFILEHEADER), 
            &nWritten, NULL);
        WriteFile(hFile, (LPVOID)&bmi, 
            sizeof(BITMAPINFOHEADER), 
            &nWritten, NULL);
        WriteFile(hFile, (LPVOID)pBits, nImageSize, 
            &nWritten, NULL);
        CloseHandle(hFile);
    }

    return TRUE;
}

Open in new window

0
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 2000 total points
ID: 33716853
It's Delphi:
http://users.telenet.be/ws36637/transparent2.html
People say to use CAPTUREBLT together with SRCCOPY in BitBlt. They also use CreateCompatibleBitmap instead of CreateDIBSection.
 
0
 
LVL 1

Author Closing Comment

by:Greg2FS
ID: 33716959
Yes just using SRCCOPY|CAPTUREBLT works !
0
 
LVL 1

Author Comment

by:Greg2FS
ID: 33716965
Thanks
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33716978
:)
I've already made a short example for you.
You are welcome.
 

#include <Windows.h>

BOOL Capture(HDC, LPRECT, LPCWSTR);

int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                      LPWSTR lpCmdLine, int nCmdShow)
{

    HWND hWnd = GetDesktopWindow();
    HDC hDC = GetDC(hWnd);

    RECT rect = { 100, 100, 500, 500 };
    HDC hDCMem = ::CreateCompatibleDC(hDC);
    HBITMAP hBitmap = ::CreateCompatibleBitmap(hDC, 500, 500);
    HBITMAP hBmpOld = (HBITMAP)::SelectObject(hDCMem, hBitmap);

    BOOL bRet = ::BitBlt(hDCMem, 0, 0, 500, 500, hDC, 100, 100, SRCCOPY | CAPTUREBLT);

    Capture(hDCMem, &rect, L"capture.bmp");

    ::SelectObject(hDCMem, hBmpOld);
    ::DeleteObject(hBitmap);
    ::DeleteDC(hDCMem);
    return 0;
}

BOOL Capture(HDC hDC, LPRECT lpRect, LPCWSTR lpszFile)
{
    BITMAPINFO bmi = { 0 };

    LPBYTE pBits = NULL;
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = lpRect->right - lpRect->left;
    bmi.bmiHeader.biHeight = lpRect->bottom - lpRect->top;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = BI_RGB;
    HBITMAP hBitmap = CreateDIBSection(hDC, &bmi, 
        DIB_RGB_COLORS, (LPVOID*)&pBits, NULL, 0);

    HDC hMemDC = CreateCompatibleDC(hDC);
    HGDIOBJ hOld = SelectObject(hMemDC, hBitmap);
    BitBlt(hMemDC, 0, 0, 
        lpRect->right - lpRect->left,
        lpRect->bottom - lpRect->top,
        hDC, lpRect->left, lpRect->top, CAPTUREBLT | SRCCOPY);
    SelectObject(hMemDC, hOld);
    DeleteDC(hMemDC);

    DWORD nImageSize = ((((bmi.bmiHeader.biWidth * 
        bmi.bmiHeader.biBitCount) 
        + 31) & ~31) >> 3) 
        * bmi.bmiHeader.biHeight;

    BITMAPFILEHEADER header = { 0 };
    header.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + 
        (DWORD)sizeof(BITMAPINFOHEADER); 
    header.bfSize = nImageSize + sizeof(BITMAPFILEHEADER) + 
        sizeof(BITMAPINFOHEADER);
    header.bfType = 0x4D42;

    HANDLE hFile = CreateFile(lpszFile, GENERIC_WRITE,
        0, NULL, CREATE_ALWAYS, 
        FILE_ATTRIBUTE_NORMAL, NULL);   

    if (hFile != INVALID_HANDLE_VALUE) 
    {
        DWORD nWritten = 0;;
        WriteFile(hFile, (LPVOID)&header, 
            sizeof(BITMAPFILEHEADER), 
            &nWritten, NULL);
        WriteFile(hFile, (LPVOID)&bmi, 
            sizeof(BITMAPINFOHEADER), 
            &nWritten, NULL);
        WriteFile(hFile, (LPVOID)pBits, nImageSize, 
            &nWritten, NULL);
        CloseHandle(hFile);
    }

    return TRUE;
}

Open in new window

capture.bmp
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question