简体   繁体   English

从像素获取RGB值并将rgb值设置回相同的像素

[英]getting a RGB value from a pixel and setting the rgb values back to the same pixel

In the code snippet below I am trying to retrieve the RGB values of a pixel in Visual C++ and then set the same RGB values back to be the same pixel. 在下面的代码片段中,我试图在Visual C ++中检索像素的RGB值,然后将相同的RGB值设置回相同的像素。 ie this is just a test. 即这只是一个测试。 However when I do it the resulting image is similar but the colors are wrong/off. 但是,当我这样做时,生成的图像是相似的,但是颜色是错误/不正确的。 The resulting image appears to be totally missing the yellow color pixels of the original image. 生成的图像似乎完全丢失了原始图像的黄色像素。 Why am I not getting the same image back after this? 为什么在此之后我仍无法获得相同的图像? Any help would be most appreciated. 非常感激任何的帮助。 Thanks 谢谢

        BYTE *pbBitmap;
    HBITMAP hDestBitmap = CreateDIBSection(memDC1, &bi, DIB_RGB_COLORS,(void**)&pbBitmap, NULL, 0);

    if (hDestBitmap){
        SelectObject(DestDC2, hDestBitmap);
        BitBlt(DestDC2, 0, 0, nX, nY, memDC1, 0, 0, SRCCOPY);

        RGBQUAD *pRGB;
        LONG nPixels;
        for (pRGB = (RGBQUAD *)pbBitmap, nPixels = (nX * nY); nPixels > 0; ++pRGB, --nPixels){
            ULONG* pSrc =(ULONG*)pRGB;
            ULONG nRed = GetRValue(*pSrc);
            ULONG nGreen = GetGValue(*pSrc);
            ULONG nBlue = GetBValue(*pSrc);

            pRGB->rgbRed=nRed;
            pRGB->rgbGreen=nGreen;
            pRGB->rgbBlue=nBlue;
        }

        CRect rctPicture2;
        m_Picture2.GetWindowRect(&rctPicture2);
        ScreenToClient(&rctPicture2);
        dc.SetStretchBltMode(COLORONCOLOR);
        StretchBlt(dc,rctPicture2.left,rctPicture2.top,rctPicture2.Width(),rctPicture2.Height(),DestDC2,0,0,1152,864,SRCCOPY);
    }

The way you are accessing the colour values is wrong. 您访问颜色值的方式是错误的。 GetRValue and other functions are meant to operate on a COLORREF value which is laid out in memory as red-green-blue-reserved , whereas the format of an RGBQUAD is blue-green-red-reserved . GetRValue和其他函数旨在对COLORREF值进行操作,该值以red-green-blue-reserved的形式布置在内存中,而RGBQUAD的格式是blue-green-red-reserved By using the Get?Value macros on an RGBQUAD , you're swapping the red and blue channels. 通过在RGBQUAD上使用Get?Value宏,您可以交换红色和蓝色通道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM