2013-12-18 20 views
0

我想在一個位圖得到位,但我不斷收到這個輸出(PS我測試了整個陣列,以及。):爲什麼所有像素值的鎖定位返回-842150451?

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

這是我的代碼來獲取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; 
} 
+5

假設'-842150451'是一個32位整數,它在十六進制中的表示是'0xcdcdcdcd'。微軟的調試'malloc'使用這個值來標記未初始化的堆內存。 http://en.wikipedia.org/wiki/Magic_number_(programming) –

回答

1

您使用的是錯誤的,它回報一個BitmapData。因此,它需要:

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

跳過錯誤檢查。

+0

你是什麼意思它返回一個BitmapData?你不是作爲一個指針傳入的嗎? – bluejamesbond

+0

哦nvm ...我明白了! – bluejamesbond

相關問題