windows 屏幕录像

头:

#ifndef __MANAGE_BMP__
#define __MANAGE_BMP__

#include <windows.h>
#include <stdint.h>
#include <cstdint>

class ManageBmp
{
public:
	ManageBmp();
	~ManageBmp();

public:
	void ShootScreen(HWND hWnd);
	long GetScreenWidth();
	long GetScreenHeight();


public:
	unsigned char* GetData();
	unsigned char* GetData(long& nLen);

private:
	long m_nOffSet = 0;
	unsigned char* m_pBmpData;

private:
	int32_t m_nScreenWidth = 0;
	int32_t m_nScreenHeight = 0;
};

#endif

源:

#include "ManageBmp.h"

ManageBmp::ManageBmp()
{
    m_pBmpData = nullptr;
    while (!m_pBmpData) {
        try {
            m_pBmpData = new unsigned char[20 * 1024 * 1024];
        }
        catch (...) {}
    }
}
ManageBmp::~ManageBmp()
{
    if (m_pBmpData) {
        delete[] m_pBmpData;
        m_pBmpData = nullptr;
    }
}

void ManageBmp::ShootScreen(HWND hWnd)
{
    HDC hdc = CreateDC(L"DISPLAY", NULL, NULL, NULL);
    
    RECT rect = { 0 };
    if (hWnd == NULL)
    {
        m_nScreenWidth = GetDeviceCaps(hdc, HORZRES);
        m_nScreenHeight = GetDeviceCaps(hdc, VERTRES);
    }
    else
    {
        GetWindowRect(hWnd, &rect);
        m_nScreenWidth = rect.right - rect.left;
        m_nScreenHeight = rect.bottom - rect.top;
    }
    HDC hmdc = CreateCompatibleDC(hdc);

    HBITMAP hBmpScreen = CreateCompatibleBitmap(hdc, m_nScreenWidth, m_nScreenHeight);
    HBITMAP holdbmp = (HBITMAP)SelectObject(hmdc, hBmpScreen);

    BITMAP bm;
    GetObject(hBmpScreen, sizeof(bm), &bm);

    BITMAPINFOHEADER bi = { 0 };
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = bm.bmWidth;
    bi.biHeight = bm.bmHeight;
    bi.biPlanes = bm.bmPlanes;
    bi.biBitCount = bm.bmBitsPixel;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = bm.bmHeight * bm.bmWidthBytes;

    BitBlt(hmdc, 0, 0, m_nScreenWidth, m_nScreenHeight, hdc, rect.left, rect.top, SRCCOPY);
    GetDIBits(hmdc, hBmpScreen, 0L, (DWORD)m_nScreenHeight, m_pBmpData + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER), (LPBITMAPINFO)&bi, (DWORD)DIB_RGB_COLORS);

    BITMAPFILEHEADER bfh = { 0 };
    bfh.bfType = ((WORD)('M' << 8) | 'B');
    bfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bi.biSizeImage;
    bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

    //拷贝bmp头
    memcpy(m_pBmpData, &bfh, sizeof(BITMAPFILEHEADER));
    memcpy(m_pBmpData + sizeof(BITMAPFILEHEADER), &bi, sizeof(BITMAPINFOHEADER));
    m_nOffSet = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bi.biSizeImage;

    //还原
    hBmpScreen = (HBITMAP)SelectObject(hmdc, holdbmp);

    DeleteDC(hdc);
    DeleteDC(hmdc);
    DeleteObject(hBmpScreen);

    //测试
    /*HANDLE hFile = CreateFile(filename, GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    DWORD dwWrite;
    WriteFile(hFile, m_pBmpData, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bi.biSizeImage, &dwWrite, NULL);
    CloseHandle(hFile);*/
}

unsigned char* ManageBmp::GetData()
{
    return m_pBmpData;
}
unsigned char* ManageBmp::GetData(long& nLen)
{
    nLen = m_nOffSet;
    return m_pBmpData;
}

long ManageBmp::GetScreenWidth()
{
    return m_nScreenWidth;
}
long ManageBmp::GetScreenHeight()
{
    return m_nScreenHeight;
}

调用:

SingleTon<ManageBmp>::GetInstance()->ShootScreen(NULL);

long nImageLen = 0;
unsigned char* pImageData = SingleTon<ManageBmp>::GetInstance()->GetData(nImageLen);
if (pImageData) {//带bmp头的数据
    //...
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值