Click to See Complete Forum and Search --> : CBitmap pixels


Wittsack
March 23rd, 2005, 10:01 AM
Hi,
I need to copy CBitmap pixels into an array of BYTE to work with them. How can this be done?

In a first step I copy the content of the client window of a MDI project into a Cbitmap:
// Snap ClientWindow as Bitmap
CBitmap bm;
CRect rect;
CClientDC dc(this);
CDC mem_dc;
GetClientRect(rect);
bm.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
mem_dc.CreateCompatibleDC(&dc);
mem_dc.SelectObject(bm);
OnDraw(&mem_dc);

Now I am looking for the bitmap dimensions and the pixel address:
// Get Bitmap Dimension and pixel start address
HBITMAP hbm = (HBITMAP)bm.GetSafeHandle();
DIBSECTION info;
GetObject(hbm, sizeof(DIBSECTION), &info);
int bm_width = info.dsBm.bmWidth;
int bm_height = info.dsBm.bmHeight;
int bm_bitsperpixel = info.dsBm.bmBitsPixel;
BYTE * pBits = (LPBYTE)info.dsBm.bmBits;

OK - and here is my problem:
BYTE * pix_array;
bit_array = new BYTE[bm_width*bm_height*bm_bitsperpixel];
for(int i=0;i<bm_width*bm_height*bm_bitsperpixel;i++){
pix_array[i] = *(pBits++);
}

When counting up pBits an access violation occurs.
What is the correct way to copy the pixels into an array?

Thanks!

Marc G
March 23rd, 2005, 02:00 PM
You can use GetDIBits to get an array of bits in the format you want.

PS: use code-tags to post code.