2

I am attempting to capture images from the screen. I currently have code which takes a screenshot and saves it to disk but I would prefer to not save it each time. After several hours of reading over other examples online I still feel I do not understand how this process works.

The two goals are to create the screen in memory to be passed to another function and to be able to capture only selected parts of the screen given (x,y) coordinates.

I am relatively new to coding so if this is a trivial thing it would not surprised but would still greatly appreciate any explanations.

Here is the sample code I found online and have been working with.

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <string>

using namespace std;

void ScreenShot()
{
    int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
    int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
    HWND hDesktopWnd = GetDesktopWindow();
    HDC hDesktopDC = GetDC(hDesktopWnd);
    HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
    HBITMAP hCaptureBitmap = CreateCompatibleBitmap(hDesktopDC,
        nScreenWidth, nScreenHeight);
    SelectObject(hCaptureDC, hCaptureBitmap);
    BitBlt(hCaptureDC, 0, 0, nScreenWidth, nScreenHeight,
        hDesktopDC, 0, 0, SRCCOPY | CAPTUREBLT);
    //SaveCapturedBitmap(hCaptureBitmap); //Place holder - Put your code here to save the captured image to disk
    ReleaseDC(hDesktopWnd, hDesktopDC);
    DeleteDC(hCaptureDC);
    DeleteObject(hCaptureBitmap);
}
Corbin
  • 21
  • 4
  • 3
    You're "repetitively new"? You mean you keep starting over, forgetting what you learned before? – Barmar Jul 28 '15 at 21:34
  • Try the [`GetPixel` function](https://msdn.microsoft.com/en-us/library/windows/desktop/dd144909(v=vs.85).aspx) – Colonel Thirty Two Jul 28 '15 at 21:35
  • I think the `hCaptureBitmap` variable contains the screen capture data. You can do whatever you want with that. – Barmar Jul 28 '15 at 21:36
  • Maybe this will help: https://msdn.microsoft.com/en-us/library/windows/desktop/dd183402(v=vs.85).aspx – Ben Jul 28 '15 at 21:38
  • 2
    It is VERY unclear what you actually want to do with your captured screen content, but as Barmar says, you have a bitmap in `hCaptureBitmap`, and it can be used to "do things" to the pixels you've captured from the screen - whatever that is. For example, you can BitBlt parts of it into yet another bitmap, perhaps? – Mats Petersson Jul 28 '15 at 21:39
  • 1
    @Barmar, Funny enough I do keep starting over after long breaks but I intended to say relatively new. – Corbin Jul 28 '15 at 22:05
  • The screenshot will be used in an openCV operation. The openCV part is working well when I test it with still frames taken and saved before running the program. Now I wish to have it run in real time. If hCaptureBitmap is where the image is stored, how would I pass it back to the calling function? – Corbin Jul 28 '15 at 22:08
  • Declare the function as returning `HBITMAP` and end it with `return hCaptureBitmap` instead of `DeleteObject(hCaptureBitmap)` – Barmar Jul 29 '15 at 15:16
  • The code is now storing the bitmap information in the HBITMAP hCaptureBitmap. Unfortunately that is only the first part of the issue. How does one put the bitmap information into an array or string? – Corbin Jul 29 '15 at 17:19

0 Answers0