头:
#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头的数据
//...
}