I am trying to get the bits in a bitmap, but I keep getting this output (PS. I tested the whole array as well):
-842150451 // Array before lockbits
-842150451 // Array after lockbits
This is my code to get the 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. 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.