The Bitmap class provides the LockBits and corresponding UnlockBits methods which enable you to fix a portion of the bitmap pixel data array in memory, access it directly and finally replace the bits in the bitmap with the modified data. LockBitsreturns a BitmapData class that describes the layout and position of the data in the locked array.
The BitmapData class contains the following important properties;
- Scan0 The address in memory of the fixed data array
- Stride The width, in bytes, of a single row of pixel data. This width is a multiple, or possiblysub-multiple, of the pixel dimensions of the image and may be padded out to include a few more bytes. I'll explain why shortly.
- PixelFormat The actual pixel format of the data. This is important for finding the right bytes
- Width The width of the locked image
- Height The height of the locked image
The relationship of Scan0 and Stride to the array in memory is shown in figure1.
Figure 1: The basic layout of a locked bitmap array
The Stride property, as shown in figure 1, holds the width of one row in bytes. The size of a row however may not be an exact multiple of the pixel size because for efficiency, the system ensures that the data is packed into rows that begin on a four byte boundary and are padded out to a multiple of four bytes. This means for example that a 24 bit per pixel image 17 pixels wide would have a stride of 52. The used data in each row would take up 3*17 = 51 bytes and the padding of 1 byte would expand each row to 52 bytes or 13*4 bytes. A 4BppIndexed image of 17 pixels wide would have a stride of 12. Nine of the bytes, or more properly eight and a half, would contain data and the row would be padded out with a further 3 bytes to a 4 byte boundary.
The data carrying portion of the row, as has been suggested above, is laid out according to the pixel format. A 24 bit per pixel image containing RGB data would have a new pixel every 3 bytes, a 32 bit per pixel RGBA every four bytes. Pixel formats that contain more than one pixel per byte, such as the 4 bit per pixel Indexed and 1 bit per pixel indexed, have to be processed carefully so that the pixel required is not confused with it's neigbour pixels in the same byte.
Finding the right byte.
Because the stride is the width of a row, to index any given row or Y coordinate you can multiply the stride by the Y coordinate to get the beginning of a particular row. Finding the correct pixel within the row is possibly more difficult and depends on knowing the layout of the pixel formats. The following examples show how to access a particular pixel for a given pixel format.
- Format32BppArgb Given X and Y coordinates, the address of the first element in the pixel is Scan0+(y * stride)+(x*4). This Points to the blue byte. The following three bytes contain the green, red and alpha bytes.
- Format24BppRgb Given X and Y coordinates, the address of the first element in the pixel is Scan0+(y*Stride)+(x*3). This points to the blue byte which is followed by the green and the red.
- Format8BppIndexed Given the X and Y coordinates the address of the byte isScan0+(y*Stride)+x. This byte is the index into the image palette.
- Format4BppIndexed Given X and Y coordinates the byte containing the pixel data is calculated as Scan0+(y*Stride)+(x/2). The corresponding byte contains two pixels, the upper nibble is the leftmost and the lower nibble is the rightmost of two pixels. The four bits of the upper and lower nibble are used to select the colour from the 16 colour palette.
- Format1BppIndexed Given the X and Y coordinates, the byte containing the pixel is calculated by Scan0+(y*Stride)+(x/8). The byte contains 8 bits, each bit is one pixel with the leftmost pixel in bit 8 and the rightmost pixel in bit 0. The bits select from the two entry colour palette.
实例程序,读取数据到
BOOL CImgProcessDlg::FillImageBuf( BYTE * pBuffer)
{
ASSERT(pBuffer);
if (!pBuffer)
return FALSE;
//IDB_BITMAP_KS是Bitmap资源,一张1024*576*8的图片
Gdiplus::Bitmap *pBitmap = Bitmap::FromResource(AfxFindResourceHandle(MAKEINTRESOURCE(IDB_BITMAP_KS), RT_BITMAP),MAKEINTRESOURCE(IDB_BITMAP_KS));
int nWidth = pBitmap->GetWidth();
int nHeight = pBitmap->GetHeight();
Rect rect( 0, 0, nWidth, nHeight );
BitmapData *pBitmapData = new BitmapData;
pBitmap->LockBits( &rect, ImageLockModeRead, pBitmap->GetPixelFormat(), pBitmapData );
BYTE *pByte = (BYTE *) pBitmapData->Scan0;
int nOffset = pBitmapData->Stride - nWidth; // 8位灰度图像
TRACE1(" pBitmapData->Stride =%d\n", nOffset );
for( int row = 0; row < nHeight; row++ )
{
for( int col = 0; col < nWidth; col++ )
{
*pBuffer = pByte[0]; //
pBuffer++;
pByte += 1;
}
pByte += nOffset;
}
pBitmap->UnlockBits( pBitmapData );
//clean:
delete pBitmapData; // 必须释放
delete pBitmap;
return 0;
}