如何将HDC位图快速复制到三维阵列?

问题描述 投票:0回答:1

我通过使用GetPixel(hdc, i, j)迭代每个像素,将来自HDC位图的图像rgb数据存储在3d数组中。

它可以工作但是这个功能非常慢。即使对于大图像(1920x1080 = 6,220,800值,不包括alpha),它也不应该花费很长时间。

我已经在网上寻找替代品,但它们都不是非常干净/可读,至少对我而言。

基本上我希望将hdc位图更快地复制到unsigned char the_image[rows][columns][3]

这是当前的代码。我需要帮助改进//store bitmap in array下的代码

// copy window to bitmap
HDC     hScreen = GetDC(window);
HDC     hDC = CreateCompatibleDC(hScreen);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, 256, 256);
HGDIOBJ old_obj = SelectObject(hDC, hBitmap);
BOOL    bRet = BitBlt(hDC, 0, 0, 256, 256, hScreen, 0, 0, SRCCOPY);

//store bitmap in array
unsigned char the_image[256][256][3];
COLORREF pixel_color;
for (int i = 0; i < 256; i++) {
    for (int j = 0; j < 256; j++) {
        pixel_color = GetPixel(hDC, i, j);
        the_image[i][j][0] = GetRValue(pixel_color);
        the_image[i][j][1] = GetGValue(pixel_color);
        the_image[i][j][2] = GetBValue(pixel_color);
    }
}

// clean up
SelectObject(hDC, old_obj);
DeleteDC(hDC);
ReleaseDC(NULL, hScreen);
DeleteObject(hBitmap);
c++ gdi
1个回答
0
投票

感谢Raymond Chen介绍了“GetDIBits”功能,以及this的其他主题,我终于设法让它工作了。

与以前相比,它几乎是瞬间完成的,虽然我遇到了一些超大堆栈大小问题,但应该是一个相当容易的修复。这是代替“//在数组中存储位图”下面的代码:

BITMAPINFO MyBMInfo = { 0 };
MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);
GetDIBits(hDC, hBitmap, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS);
MyBMInfo.bmiHeader.biBitCount = 24;
MyBMInfo.bmiHeader.biCompression = BI_RGB;
MyBMInfo.bmiHeader.biHeight = abs(MyBMInfo.bmiHeader.biHeight);
unsigned char the_image[256][256][3];
GetDIBits(hDC, hBitmap, 0, MyBMInfo.bmiHeader.biHeight,
    &the_image[0], &MyBMInfo, DIB_RGB_COLORS);
© www.soinside.com 2019 - 2024. All rights reserved.