Skip to content

Instantly share code, notes, and snippets.

@maaatts
Created August 4, 2016 12:50
Show Gist options
  • Save maaatts/e33d45eaae246e471ceb7af278bb6322 to your computer and use it in GitHub Desktop.
Save maaatts/e33d45eaae246e471ceb7af278bb6322 to your computer and use it in GitHub Desktop.
win32 stinks
#include "Screenshot.hpp"
Screenshot::Screenshot(int x, int y, int w, int h) : x(x), y(y), width(w), height(h)
{
HBITMAP hBitmap;
hdcSys = GetDC(NULL);
hdcMem = CreateCompatibleDC(hdcSys);
void *ptrBitmapPixels;
BITMAPINFO bi; HDC hdc;
ZeroMemory(&bi, sizeof(BITMAPINFO));
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = getWidth();
bi.bmiHeader.biHeight = -getHeight();
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
hdc = GetDC(NULL);
hBitmap = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, &ptrBitmapPixels, NULL, 0);
SelectObject(hdcMem, hBitmap);
this->mat = cv::Mat(getHeight(), getWidth(), CV_8UC4, ptrBitmapPixels, 0);
}
Screenshot::~Screenshot()
{
// TODO: dtor()
}
cv::Mat Screenshot::getMat()
{
BitBlt(hdcMem, this->getX(), this->getY(), this->getWidth(), this->getHeight(), hdcSys, 0, 0, SRCCOPY);
return this->mat;
}
#pragma once
#include <opencv2/core.hpp>
#include <Windows.h>
class Screenshot {
private:
HDC hdcSys, hdcMem;
int x, y, width, height;
cv::Mat mat;
public:
Screenshot(int x, int y, int w, int h);
~Screenshot();
inline int getX() { return x; }
inline int getY() { return y; }
inline int getWidth() { return width; }
inline int getHeight() { return height; }
cv::Mat getMat();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment