65.9K
CodeProject is changing. Read more.
Home

Capture Program using GDI

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.85/5 (23 votes)

Nov 11, 2015

CPOL

1 min read

viewsIcon

37196

downloadIcon

6288

Capture program using GDI

Source files were corrupted. I uploaded sources again.

Introduction

I have been interested in WTL (Windows Template Library) for a very long time. So, I decided to make a small program using WTL last September. I really like the fact that the size of the executable files that WTL makes is much smaller than the ones that MFC (Microsoft Foundation Classes) makes and the source code is open for everyone to see. Some people might think that there are no advantages between WTL and MFC. But I believe that these tools are both powerful for anyone who wants to build small, fast programs.

If some errors show up related to libraries, you might have to install the run-time components below.

Visual C++ Redistributable for Visual Studio 2015

I have tested this program on Windows 7 and Windows XP. I don’t know if it will work on other Windows platforms.

Features

  1. Capture Full Screen - You can take a snapshot of the whole screen.
  2. Capture Window - You can capture one window on your screen.
  3. Capture Region - You can specify what you want to capture.
  4. Zoom In
  5. Zoom Out
  6. Copy the captured Image to MS Paint
  7. Favorites
  8. Copy the captured image to the Clipboard
  9. Environment (Hotkeys)
  10. Image viewer - includes panning function

Implementation

Sending the Captured Image to Microsoft Paint

    //Copy an image to the clipboard.
    if (OpenClipboard())
    {
        EmptyClipboard();
        CBitmap bmpCopy;

        BITMAP bm;
        if (GetObject(pImageData->m_bmpOri.m_hBitmap, sizeof(BITMAP), &bm))
            bmpCopy = (HBITMAP)CopyImage(pImageData->m_bmpOri.m_hBitmap, IMAGE_BITMAP, 0, 0, 0);
        ATLASSERT(::GetObjectType(bmpCopy.m_hBitmap) == OBJ_BITMAP);
        SetClipboardData(CF_BITMAP, bmpCopy.m_hBitmap);
        CloseClipboard();
    }

    if (IsClipboardFormatAvailable(CF_BITMAP))
    {
        HWND                    hPaint;
        STARTUPINFO                si;
        PROCESS_INFORMATION        pi;

        memset(&si, 0, sizeof(STARTUPINFO));
        memset(&pi, 0, sizeof(PROCESS_INFORMATION));
        si.cb = sizeof(STARTUPINFO);

        wchar_t    szWindowDirector[MAX_PATH] = { 0 };
        CString    strMspaintLoc;

        GetSystemDirectory(szWindowDirector, MAX_PATH);
        strMspaintLoc.Format(L"%s\\%s", szWindowDirector, L"mspaint.exe");
        if (CreateProcess(strMspaintLoc,
            NULL, NULL, NULL, FALSE,
            NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi))
        {
            //HWINEVENTHOOK hook =
            //    SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_CREATE, NULL,
            //        WinEventProc, 0, 0, WINEVENT_OUTOFCONTEXT);
            /*ResumeThread(pi.hThread);*/
            //THIS PART NEEDS TO BE FIXED.
            while (TRUE)
            {
                Sleep(100);
                hPaint = FindWindow(L"MsPaintApp", NULL);
                ATLTRACE(L"findwindow Handle %X\n", hPaint);
                if (hPaint) break;
            }
        }
        else
        {
            MessageBox(L"Couldn't find mspaint program");
            return;
        }

        if (OpenClipboard())
        {
            CloseClipboard();

            if (SetForegroundWindow(hPaint))
            {
                //Ctrl + V (paste)
                INPUT ip;
                ip.type = INPUT_KEYBOARD;
                ip.ki.wScan = 0;
                ip.ki.time = 0;
                ip.ki.dwExtraInfo = 0;

                // Press the "Ctrl" key
                ip.ki.wVk = VK_CONTROL;
                ip.ki.dwFlags = 0; // 0 for key press
                SendInput(1, &ip, sizeof(INPUT));

                // Press the "V" key
                ip.ki.wVk = 'V';
                ip.ki.dwFlags = 0; // 0 for key press
                SendInput(1, &ip, sizeof(INPUT));

                // Release the "V" key
                ip.ki.wVk = 'V';
                ip.ki.dwFlags = KEYEVENTF_KEYUP;
                SendInput(1, &ip, sizeof(INPUT));

                // Release the "Ctrl" key
                ip.ki.wVk = VK_CONTROL;
                ip.ki.dwFlags = KEYEVENTF_KEYUP;
                SendInput(1, &ip, sizeof(INPUT));
            }
        }
    }

Capturing Screen

    CRect        rc;
    ShowWindow(SW_MINIMIZE);
    Sleep(100);

    // Start to get an Image from the nearest screen (Prepare capture data)
    CDC            dcWindow;
    dcWindow.CreateDCW(L"DISPLAY", NULL, NULL, NULL);

    // Get the nearest monitor rect
    CMonitorInfo::GetMonitorRect(m_hWnd, rc);

    CBitmap        bmp, oldbmp;
    CDC            dcMem;
    CClientDC    dc(m_hWnd);
    dcMem.CreateCompatibleDC();
    bmp.CreateCompatibleBitmap(dcWindow, rc.Width(), rc.Height());
    oldbmp = dcMem.SelectBitmap(bmp);
    dcMem.BitBlt(0, 0, rc.Width(), rc.Height(), dcWindow, rc.left, rc.top, SRCCOPY);

    SetCaptureImage(&bmp);

    dcMem.SelectBitmap(oldbmp);
    ShowWindow(SW_RESTORE);

Capturing a Window

A Window shows up covering your screen after the whole screen has been copied.

void CMCFrameWnd::CaptureWindow()
{
    // Get full screen size.
    CRect        rcMonitor;

    ShowWindow(SW_MINIMIZE);
    Sleep(100);

    // Start to get an Image from the nearest screen (Prepare capture data)
    CDC            dcWindow;
    dcWindow.CreateDCW(L"DISPLAY", NULL, NULL, NULL);

    // Get the nearest monitor Rect
    CMonitorInfo::GetMonitorRect(m_hWnd, rcMonitor);

    CBitmap        bmp, oldbmp;
    CDC            dcMem;
    CClientDC    dc(m_hWnd);
    dcMem.CreateCompatibleDC();
    bmp.CreateCompatibleBitmap(dcWindow, rcMonitor.Width(), rcMonitor.Height());
    oldbmp = dcMem.SelectBitmap(bmp);
    dcMem.BitBlt(0, 0, rcMonitor.Width(),
    rcMonitor.Height(), dcWindow, rcMonitor.left, rcMonitor.top, SRCCOPY);
    dcMem.SelectBitmap(oldbmp);

    if (m_pwndTemparyScreenWnd != NULL)
        delete m_pwndTemparyScreenWnd;
    m_pwndTemparyScreenWnd = new CTemparyScreenWnd(this);
    m_pwndTemparyScreenWnd->mode = CTemparyScreenWnd::Mode::TEMPARYSCREEN_WINDOW;
    m_pwndTemparyScreenWnd->Create(m_hWnd, rcMonitor, 0, WS_POPUP | WS_VISIBLE, WS_EX_TOPMOST);
    m_pwndTemparyScreenWnd->GetWindowData();
    m_pwndTemparyScreenWnd->SetScreenImage(bmp);
    m_pwndTemparyScreenWnd->ShowMonitorScreen();
    SetForegroundWindow(m_pwndTemparyScreenWnd->m_hWnd);
}