gpt4 book ai didi

c++ 使用 GetDIBits() 读取像素

转载 作者:太空宇宙 更新时间:2023-11-04 15:18:18 25 4
gpt4 key购买 nike

我正在尝试创建一个与 Windows API GetPixel() 函数等效的函数,但我想创建我的屏幕的位图,然后读取该缓冲区。

这就是我得到的(大部分是从谷歌搜索中复制粘贴的),当我运行它时它只打印出 0。我想我大部分都做对了,我的问题是我不知道如何读取 BYTE 变量。

所以我的问题是,我需要做什么才能让它用我的 for 循环打印出一些随机颜色(R、G 或 B)?

#include <Windows.h>
#include <iostream>
#include <math.h>
#include <stdio.h>

using namespace std;

int main() {

HDC hdc,hdcMem;

hdc = GetDC(NULL);
hdcMem = CreateCompatibleDC(hdc);

HBITMAP hBitmap = CreateCompatibleBitmap(hdc, 1680, 1050);

BITMAPINFO MyBMInfo = {0};
MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);
// Get the BITMAPINFO structure from the bitmap
if(0 == GetDIBits(hdcMem, hBitmap, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS)) {
cout << "error" << endl;
}

// create the bitmap buffer
BYTE* lpPixels = new BYTE[MyBMInfo.bmiHeader.biSizeImage];

MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);
MyBMInfo.bmiHeader.biBitCount = 32;
MyBMInfo.bmiHeader.biCompression = BI_RGB;
MyBMInfo.bmiHeader.biHeight = abs(MyBMInfo.bmiHeader.biHeight);

// get the actual bitmap buffer
if(0 == GetDIBits(hdc, hBitmap, 0, MyBMInfo.bmiHeader.biHeight, (LPVOID)lpPixels, &MyBMInfo, DIB_RGB_COLORS)) {
cout << "error2" << endl;
}

for(int i = 0; i < 100; i++) {
cout << (int)lpPixels[i] << endl;
}

return 0;
}
  • Windows 7
  • C::B 13.12(控制台应用程序)
  • 编译器:mingw32-gcc
  • 库 gdi32 链接

最佳答案

按照约定,我将使用工作代码片段添加一个新答案(我添加了 lpPixels 缺失的清理)。请参阅我之前的回答和@enhzflep 所做的讨论。

#include <Windows.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;

HBITMAP GetScreenBmp( HDC hdc) {
// Get screen dimensions
int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);

// Create compatible DC, create a compatible bitmap and copy the screen using BitBlt()
HDC hCaptureDC = CreateCompatibleDC(hdc);
HBITMAP hBitmap = CreateCompatibleBitmap(hdc, nScreenWidth, nScreenHeight);
HGDIOBJ hOld = SelectObject(hCaptureDC, hBitmap);
BOOL bOK = BitBlt(hCaptureDC,0,0,nScreenWidth, nScreenHeight, hdc,0,0,SRCCOPY|CAPTUREBLT);

SelectObject(hCaptureDC, hOld); // always select the previously selected object once done
DeleteDC(hCaptureDC);
return hBitmap;
}

int main() {
HDC hdc = GetDC(0);

HBITMAP hBitmap = GetScreenBmp(hdc);

BITMAPINFO MyBMInfo = {0};
MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);

// Get the BITMAPINFO structure from the bitmap
if(0 == GetDIBits(hdc, hBitmap, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS)) {
cout << "error" << endl;
}

// create the bitmap buffer
BYTE* lpPixels = new BYTE[MyBMInfo.bmiHeader.biSizeImage];

// Better do this here - the original bitmap might have BI_BITFILEDS, which makes it
// necessary to read the color table - you might not want this.
MyBMInfo.bmiHeader.biCompression = BI_RGB;

// get the actual bitmap buffer
if(0 == GetDIBits(hdc, hBitmap, 0, MyBMInfo.bmiHeader.biHeight, (LPVOID)lpPixels, &MyBMInfo, DIB_RGB_COLORS)) {
cout << "error2" << endl;
}

for(int i = 0; i < 100; i++) {
cout << (int)lpPixels[i];
}

DeleteObject(hBitmap);
ReleaseDC(NULL, hdc);
delete[] lpPixels;
return 0;
}

关于c++ 使用 GetDIBits() 读取像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26233848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com