简体   繁体   English

为什么对于所有像素值,锁定位都返回-842150451?

[英]Why is locked bits returning -842150451 for all pixel values?

I am trying to get the bits in a bitmap, but I keep getting this output (PS. I tested the whole array as well): 我试图获取位图中的位,但仍保持此输出(PS。我也测试了整个数组):

-842150451 // Array before lockbits
-842150451 // Array after  lockbits

This is my code to get the lockedBits. 这是我的代码,用于获取lockedBits。

BitmapData * getLockedBitmapData()
{
    float squareSideLength = 50 * 4;

    Bitmap * src = new Bitmap(squareSideLength , squareSideLength);    
    Graphics * graphics = Graphics::FromImage(solid);
    SolidBrush blackBrush(Color(255, 0, 0, 0));

    graphics->FillRectangle(&blackBrush, FLOAT_ZERO, FLOAT_ZERO, squareSideLength, squareSideLength);

    int srcWidth = src->GetWidth();
    int srcHeight = src->GetHeight();

    UINT * pixels = new UINT[srcWidth * srcHeight];

    // _RPT1(0, "%d\n", pixels[55]);

    BitmapData * bitmapData = new BitmapData();
    bitmapData->Width = srcWidth;
    bitmapData->Height = srcHeight;
    bitmapData->Stride = 4 * srcWidth;
    bitmapData->PixelFormat = PixelFormat32bppARGB;
    bitmapData->Scan0 = (VOID*) pixels;
    bitmapData->Reserved = NULL;

    src->LockBits(new Rect(0, 0, srcWidth, srcHeight), 
                  ImageLockMode::ImageLockModeRead | ImageLockMode::ImageLockModeWrite,
                  src->GetPixelFormat(), 
                  bitmapData);

    // _RPT1(0, "%d\n", pixels[55]);

    return bitmapData;
}

You are using it wrong, it returns a BitmapData. 您使用错误,它返回一个BitmapData。 So it needs to be: 因此它必须是:

BitmapData bitmapData;
Status ret = src->LockBits(new Rect(0, 0, srcWidth, srcHeight), 
               ImageLockMode::ImageLockModeRead | ImageLockMode::ImageLockModeWrite,
               src->GetPixelFormat(), 
               &bitmapData);
if (ret != Ok) {
   // Report error
   //...
}

Not not skip error checking. 不是不能跳过错误检查。

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

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