64位和32位C ++上的BITMAPDATA问题

时间:2017-08-13 20:13:44

标签: c++ bitmap bitmapdata

我正在使用c ++开发一个应用程序 我在尝试捕捉屏幕时遇到问题。然后编辑一些像素 并保存图像

当我选择平台作为Win32时,我的代码工作得非常好 但是一旦我将平台从Win32更改为x64,代码就会失败

尝试访问像素时,它开始提供访问冲突 我在两个平台上检查过,int的大小是4个字节,imageData.Stride的大小是-5528 当我这样做(行*步幅/ 4 + col)时,我在两个平台上得到相同的值

imageData.getPixelFormat()返回139273,即PixelFormat32bppRGB 在两个平台下

我发布以下代码 请帮助我,我做了很多谷歌,但没有任何帮助

访问冲突错误来自此行 UINT curColor = pixels [row * iStride / 4 + col]; 当行值> 0

void BitmapToJpg(HBITMAP hbmpImage, int width, int height)
{
    p_bmp = Bitmap::FromHBITMAP(hbmpImage, NULL);


    CLSID pngClsid;
    int result = GetEncoderClsid(L"image/jpeg", &pngClsid);
    if (result != -1)
        std::cout << "Encoder succeeded" << std::endl;
    else
        std::cout << "Encoder failed" << std::endl;



//***************************Testing Lockbits********************************//
// successfull results and position is also correct
BitmapData imageData;
Rect rect(0, 0, width, height);

p_bmp->LockBits(
    &rect,
    ImageLockModeWrite,
    p_bmp->GetPixelFormat(),
    //PixelFormat24bppRGB,
    &imageData);

cout << p_bmp->GetPixelFormat();

UINT* pixels;
pixels = (UINT*)imageData.Scan0;
int iStride = imageData.Stride;

int x = sizeof(int);
byte red = 0;
byte green = 0;
byte blue = 255;
byte alpha = 0;
for (int row = 0; row < height; ++row)
{
    for (int col = 0; col < width; ++col)
    {
        ///Some code to get color

        UINT curColor = pixels[row * iStride / 4 + col];
        int b = curColor & 0xff;
        int g = (curColor & 0xff00) >> 8;
        int r = (curColor & 0xff0000) >> 16;
        int a = (curColor & 0xff000000) >> 24;
        //result_pixels[col][row] = RGB(r, g, b);
        if (b>15 && b < 25 && g<5 && r>250)
        {
            //Red found

            //Code to change color, generate ARGB from provided RGB values
            UINT32 rgb = (alpha << 24) + (red << 16) + (green << 8) + (blue);

             curColor = rgb;
             b = curColor & 0xff;
             g = (curColor & 0xff00) >> 8;
             r = (curColor & 0xff0000) >> 16;
             a = (curColor & 0xff000000) >> 24;


            cout << "Red found" << endl;
            pixels[row * iStride / 4 + col]=rgb;
        }

    }
}

p_bmp->UnlockBits(&imageData);
//*****************************Till Here*************************************//

p_bmp->Save(L"screen.jpg", &pngClsid, NULL);
delete p_bmp;
}

0 个答案:

没有答案