About glReadPixels

I tried to use glReadPixels to read the color(data) information of a rectangle area into a data buffer and in order to create a bitmap file. However, the program crashes. Any one can help me solve the problem? Thank you very much. Here is the code:
gl.m_pBitmapData = new UINT[nWidth*nHeight];
glReadPixels(StartX, StartY, nWidth, nHeight, GL_RGBA, GL_UNSIGNED_INT, gl.m_pBitmapData);

Multiply by 4!

new UINT[nWidth*nHeight * 4 ];

or if one byte per channel is sufficient to you, then read unsigned bytes.
glReadPixels(StartX, StartY, nWidth, nHeight, GL_RGBA, GL_UNSIGNED_BYTE, gl.m_pBitmapData);

The type that goes to glReadPixels is for one channel. You have 4 channels for RGBA.

Thanks. For each pixel, the data is in 4 BYTEs when I use GL_RGBA, right? But my buffer is in UINT, it is already 4 BYTEs. So it should be OK, right?
Buffer size is: nWidthnHeight(UINTs) = nWidth nHeight4(BYTEs)
Data size is: nWidth
nHeight*4(BYTEs)

The type that goes to glReadPixels is for one channel. You have 4 channels for RGBA.
Oh, I see, Thank you!

You might need to have to specify the packing parameters:
glPixelStorei(GL_PACK_ROW_LENGTH, nWidth);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
prior to the call to glReadPixels().

Maybe the pack alignment can be 4?

Originally posted by Joseph Steel:
[b] You might need to have to specify the packing parameters:
glPixelStorei(GL_PACK_ROW_LENGTH, nWidth);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
prior to the call to glReadPixels().

Maybe the pack alignment can be 4? [/b]
Thank you for your reminding.