从HBITMAP获取特定像素的RGB值

亚当·林德伯格

如何从HBITMAP获取一个特定的像素RGB值?我尝试在StackOverflow上阅读类似的帖子,但没有一个真正适合这个问题。下面的代码似乎在HBITMAP中获得了另一个位置(而不是想要的位置)的RGB值。

    int width = rc.right - rc.left;
    int height = rc.bottom - rc.top;

    HDC hdcSource = hdc; // the source device context
    HBITMAP hSource = hbmp; // the bitmap selected into the device context

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

    // Get the BITMAPINFO structure from the bitmap
    if(0 == GetDIBits(hdcSource, hSource, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS))
    {
        // error handling
    }

    // create the pixel buffer
    BYTE* Pixels = new BYTE[MyBMInfo.bmiHeader.biSizeImage];

    // We'll change the received BITMAPINFOHEADER to request the data in a
    // 32 bit RGB format (and not upside-down) so that we can iterate over
    // the pixels easily. 

    // requesting a 32 bit image means that no stride/padding will be necessary,
    // although it always contains an (possibly unused) alpha channel
    MyBMInfo.bmiHeader.biBitCount = 32;
    MyBMInfo.bmiHeader.biCompression = BI_RGB;  // no compression -> easier to use
    // correct the bottom-up ordering of lines (abs is in cstdblib and stdlib.h)
    MyBMInfo.bmiHeader.biHeight = abs(MyBMInfo.bmiHeader.biHeight);

    // Call GetDIBits a second time, this time to (format and) store the actual
    // bitmap data (the "pixels") in the buffer lpPixels
    if(0 == GetDIBits(hdcSource, hSource, 0, MyBMInfo.bmiHeader.biHeight,
                      Pixels, &MyBMInfo, DIB_RGB_COLORS))
    {
        // error handling
    }

    int PixelX = 221;
    int PixelY = 14;

    cout << "R: " << (int)Pixels[4*((PixelX-1)+(PixelY-1)*width)] << " | G: " << (int)Pixels[4*((PixelX-1)+(PixelY-1)*width)+1] << " | B: " << (int)Pixels[4*((PixelX-1)+(PixelY-1)*width)+2] << endl;

编辑(使用user1118321的解决方案)

高度为1,因为y从0而不是1开始。

cout << "R: " << (int)Pixels[4*((PixelX)+(height-1-(PixelY))*width)] << " | G: " << (int)Pixels[4*((PixelX)+(height-1-(PixelY))*width)+1] << " | B: " << (int)Pixels[4*((PixelX)+(height-1-(PixelY))*width)+2] << endl;
用户名

Windows位图从左下角开始存储其像素,Y向上增大。尝试使用((height - 1) - PixelY)而不是正义PixelY,看看是否有帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

获取每个超像素的RGB像素值列表

来自分类Dev

在openCV中设置特定图像中像素的RGB值

来自分类Dev

如何使用Qt获取BMP中每个像素的RGB值?

来自分类Dev

Fabricjs 在缩放或平移后获取 RGB 像素值

来自分类Dev

从像素的十六进制获取 rgb 值(0..255)

来自分类Dev

c ++从hbitmap获取原始像素数据

来自分类Dev

读取像素并找到特定的RGB

来自分类Dev

读取像素并找到特定的RGB

来自分类Dev

像素RGB值全为零

来自分类Dev

如何设置像素的RGB值

来自分类Dev

获取像素Alpha值

来自分类Dev

在python中为具有特定颜色的像素提供不同的RGB值

来自分类Dev

在鼠标移动时获取fabric.js中图像像素的rgb值

来自分类Dev

如何使用 Wpf、MVVM 获取图像像素位置和 RGB 值?

来自分类Dev

通过CGBitmapContextCreate错误获取像素RGB

来自分类Dev

获取 stb_image 中像素的 RGB

来自分类Dev

如何获取图像中特定直线的RGB值?

来自分类Dev

更改RGB图像中多个像素的值

来自分类Dev

如何获得GD像素的正确rgb值?

来自分类Dev

计算椭圆中的平均像素RGB值

来自分类Dev

如何从亮度计算像素的RGB值?

来自分类Dev

如何访问RGB图像的每个像素值?

来自分类Dev

更改RGB图像中多个像素的值

来自分类Dev

48bit RGB 单像素值

来自分类Dev

OpenCV获取像素颜色值

来自分类Dev

OpenCV获取像素颜色值

来自分类Dev

MATLAB获取xyrgb像素值

来自分类Dev

Matlab:使用特定的RGB找到二维的最小像素

来自分类Dev

如何从RGB值获取CGColor?