【问题标题】:How can an HDC bitmap be copied to a 3-dimensional array quickly?如何快速将 HDC 位图复制到 3 维数组?
【发布时间】:2019-09-18 10:53:56
【问题描述】:

我通过使用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);

【问题讨论】:

  • 使用GetDIBits批量提取像素数据。
  • GetDIBits,如果您想了解更多信息。
  • 如果不是,请在启用优化的情况下编译代码。

标签: c++ gdi


【解决方案1】:

感谢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);

【讨论】:

    猜你喜欢
    • 2010-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多