如何从桌面中获取一块区域,然后计算各种颜色的个数
发布网友
发布时间:2022-05-22 02:56
我来回答
共1个回答
热心网友
时间:2024-03-06 00:17
//GrabScreenToBitmap.h
#ifndef GRABSCREENTOBITMAP_H
#define GRABSCREENTOBITMAP_H
HBITMAP GrabScrToBmp(LPRECT lpRect);
BOOL CopyBmpToClipBoard(const HBITMAP hBmp);
BOOL SaveBmpToFile(const HBITMAP hBmp, const LPCSTR lpFileName);
#endif
//GrabScreenToBitmap.cpp
HBITMAP GrabScrToBmp(LPRECT lpRect) //lpRect 代表选定区域
{
if (IsRectEmpty(lpRect))
{
return NULL;
}
HDC hScrDC; // 屏幕设备描述表句柄
HDC hMemDC; //内存设备描述表句柄
HBITMAP hBmp; // 位图句柄
HBITMAP hOldBmp;
int nX, nY, nX2, nY2; // 选定区域坐标
int nWidth, nHeight; // 位图宽度和高度
int xScrn, yScrn; // 屏幕分辨率
hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
hMemDC = CreateCompatibleDC(hScrDC);
nX = lpRect->left;
nY = lpRect->top;
nX2 = lpRect->right;
nY2 = lpRect->bottom;
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
if (nX < 0)
{
nX = 0;
}
if (nY < 0)
{
nY = 0;
}
if (nX2 > xScrn)
{
nX2 = xScrn;
}
if (nY2 > yScrn)
{
nY2 = yScrn;
}
nWidth = nX2 - nX;
nHeight = nY2 - nY;
hBmp = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
hOldBmp = (HBITMAP) SelectObject(hMemDC, hBmp);
BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
hBmp = (HBITMAP) SelectObject(hMemDC, hOldBmp);
DeleteDC(hScrDC);
DeleteDC(hMemDC);
return hBmp;
}
BOOL CopyBmpToClipBoard(const HBITMAP hBmp)
{
if (OpenClipboard(GetActiveWindow()))
{
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBmp);
CloseClipboard();
return true;
}
return false;
}
BOOL SaveBmpToFile(const HBITMAP hBmp, const LPCSTR lpFileName)
{
HDC hDC;
int nBits;
WORD wBitCount;
DWORD dwPaletteSize = 0;
DWORD dwBmpBitSize;
DWORD dwWritten;
BITMAP bmp;
BITMAPFILEHEADER bmpFileHdr;
BITMAPINFO bmpInfo;
LPBITMAPINFO lpBmpInfo;
HANDLE hFile, hDib, hPal, hOldPal = NULL;
hDC = CreateDC("Display", NULL, NULL, NULL);
nBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
DeleteDC(hDC);
if (nBits <= 1)
{
wBitCount = 1;
}
else if (nBits <= 4)
{
wBitCount =4;
}
else if (nBits <= 8)
{
wBitCount = 8;
}
else if (nBits <= 16)
{
wBitCount = 16;
}
else if (nBits <= 24)
{
wBitCount = 24;
}
else
{
wBitCount = 32;
}
if (wBitCount <= 8)
{
dwPaletteSize = 1 << wBitCount * sizeof(RGBQUAD);
}
GetObject(hBmp, sizeof(BITMAP), &bmp);
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = bmp.bmWidth;
bmpInfo.bmiHeader.biHeight = bmp.bmHeight;
bmpInfo.bmiHeader.biPlanes = bmp.bmPlanes;
bmpInfo.bmiHeader.biBitCount = wBitCount;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biSizeImage = 0;
bmpInfo.bmiHeader.biXPelsPerMeter = 0;
bmpInfo.bmiHeader.biYPelsPerMeter = 0;
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biClrImportant = 0;
dwBmpBitSize = (bmp.bmWidth * wBitCount + 31) / 32 * 4 * bmp.bmHeight;
hDib = GlobalAlloc(GHND, sizeof(BITMAPINFOHEADER) + dwBmpBitSize + dwPaletteSize);
lpBmpInfo = (LPBITMAPINFO) GlobalLock(hDib);
*lpBmpInfo = bmpInfo;
hPal = GetStockObject(DEFAULT_PALETTE);
if (hPal)
{
hDC = GetDC(GetActiveWindow());
hOldPal = SelectPalette(hDC, (HPALETTE) hPal, FALSE);
RealizePalette(hDC);
}
GetDIBits(hDC, hBmp, 0, (UINT)bmp.bmHeight,
(LPSTR)lpBmpInfo + sizeof(BITMAPINFOHEADER) + dwPaletteSize, (LPBITMAPINFO) lpBmpInfo, DIB_RGB_COLORS);
if (hOldPal)
{
SelectPalette(hDC, (HPALETTE) hOldPal, TRUE);
ReleaseDC(GetActiveWindow(), hDC);
RealizePalette(hDC);
}
hFile = CreateFile(lpFileName,
GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
bmpFileHdr.bfType = 0x4d42;
bmpFileHdr.bfSize = sizeof(BITMAPFILEHEADER)
+ sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmpBitSize;
bmpFileHdr.bfReserved1 = 0;
bmpFileHdr.bfReserved2 = 0;
bmpFileHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER)
+ (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize;
WriteFile(hFile, (LPSTR)&bmpFileHdr,
(DWORD)sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
WriteFile(hFile, (LPSTR)lpBmpInfo,
(DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmpBitSize, &dwWritten, NULL);
GlobalUnlock(hDib);
GlobalFree(hDib);
CloseHandle(hFile);
return TRUE;
}