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!
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!