Click here to Skip to main content
15,919,245 members

Comments by Member 11545824 (Top 2 by date)

Member 11545824 2-May-15 8:25am View    
i have it.
please see these function:
<pre lang="c++">BYTE* Get24BitPixels(HBITMAP pBitmap, WORD *pwWidth, WORD *pwHeight)
{
// a bitmap object just to get bitmap width and height
BITMAP bmpBmp;

// pointer to original bitmap info
LPBITMAPINFO pbmiInfo;

// bitmap info will hold the new 24bit bitmap info
BITMAPINFO bmiInfo;

// width and height of the bitmap
WORD wBmpWidth, wBmpHeight;

// ---------------------------------------------------------
// get some info from the bitmap
// ---------------------------------------------------------
GetObject(pBitmap, sizeof(bmpBmp),&bmpBmp);
pbmiInfo = (LPBITMAPINFO)&bmpBmp;

// get width and height
wBmpWidth = (WORD)pbmiInfo->bmiHeader.biWidth;
wBmpWidth -= (wBmpWidth%4); // width is 4 byte boundary aligned.
wBmpHeight = (WORD)pbmiInfo->bmiHeader.biHeight;

// copy to caller width and height parms
*pwWidth = wBmpWidth;
*pwHeight = wBmpHeight;
// ---------------------------------------------------------

// allocate width * height * 24bits pixels
BYTE *pPixels = new BYTE[wBmpWidth*wBmpHeight*3];
if (!pPixels) return NULL;

// get user desktop device context to get pixels from
HDC hDC = GetWindowDC(NULL);

// fill desired structure
bmiInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmiInfo.bmiHeader.biWidth = wBmpWidth;
bmiInfo.bmiHeader.biHeight = -wBmpHeight;
bmiInfo.bmiHeader.biPlanes = 1;
bmiInfo.bmiHeader.biBitCount = 24;
bmiInfo.bmiHeader.biCompression = BI_RGB;
bmiInfo.bmiHeader.biSizeImage = wBmpWidth*wBmpHeight*3;
bmiInfo.bmiHeader.biXPelsPerMeter = 0;
bmiInfo.bmiHeader.biYPelsPerMeter = 0;
bmiInfo.bmiHeader.biClrUsed = 0;
bmiInfo.bmiHeader.biClrImportant = 0;

// get pixels from the original bitmap converted to 24bits
int iRes = GetDIBits(hDC,pBitmap,0,wBmpHeight,(LPVOID)pPixels,&bmiInfo,DIB_RGB_COLORS);

// release the device context
ReleaseDC(NULL,hDC);

// if failed, cancel the operation.
if (!iRes)
{
delete pPixels;
return NULL;
};

// return the pixel array
return pPixels;
}</pre>
do you see a memory leak with new\delete?
why? because 1 external program test the code and give me an error
Member 11545824 4-Apr-15 7:21am View    
i'm sorry.. i'm totaly new with these forum