views:

39

answers:

1

I'm porting an application from Windows Mobile 2003 to Windows Mobile 6, under Visual Studio 2008. The target device has a VGA resolution screen, and I was surprised to find that the following code fails;

CClientDC ClientDC(this);
 CRect Rect;
 GetClientRect(&Rect);

 int nWidth = Rect.Width(),nHeight = Rect.Height();
 CBitmap Temp;
 if (!Temp.CreateCompatibleBitmap(&ClientDC,nWidth,nHeight))
 {
  LogError(elvl_Debug,_T("Error creating bitmap (%s)"),LastSysError());

 } else
 {
  BITMAP bmpinfo;
  Temp.GetBitmap(&bmpinfo);
 }

The return code from CreateCompatibleBitmap is 8, which translates to 'Not enough memory to process command. nWidth is 350, nHeight is 400, and the display is 16 bits per pixel, so my bitmap is a whopping 280K. The device I'm using has 256mb of program memory, and I've told the linker to reserve 4mb of stack and 64mb of heap. Any ideas what I'm doing wrong, and more importantly a solution? I've been using code similar to the above on Windows CE since CE 2.1 with no problems.

Edit: As per Josh Kelly's post, I moved to device independent bitmaps which works fine on the device. Code is now something like this

CClientDC ClientDC(this);
CRect Rect;
GetClientRect(&Rect);
int nWidth = Rect.Width(),nHeight = Rect.Height();
BITMAPINFOHEADER bmi = { sizeof(bmi) }; 
bmi.biWidth = nWidth; 
bmi.biHeight = nHeight; 
bmi.biPlanes = 1; 
bmi.biBitCount = 8; 
HDC hdc = CreateCompatibleDC(NULL); 
BYTE* pbData = 0; 
HBITMAP DIB = CreateDIBSection(hdc, (BITMAPINFO*)&bmi, DIB_RGB_COLORS, (void**)&pbData, NULL, 0);
CBitmap *pTempBitmap = CBitmap::FromHandle(DIB);
+2  A: 

I haven't done any Windows CE / Windows Mobile programming, but I have dealt with a similar problem (CreateCompatibleBitmap failing with ERROR_NOT_ENOUGH_MEMORY) in desktop Windows. Apparently, from what I've been able to tell from looking around online, Windows may enforce global limitations on the available memory for device dependent bitmaps. (For example, some video drivers may choose to store device dependent bitmaps in video RAM, in which case you're limited by how much RAM is on your video card.) See, for example, this thread. From what I can tell, these limits are determined by the individual video cards or drivers; some computers' storage may be effectively unlimited, others may have strict limits.

One solution is to use device independent bitmaps instead, even though they have a slight performance penalty.

Josh Kelley
This looks likely to be the case, as CreateBitMap didn't work either. I'll try CreateDIBSection and see how that fares.
Shane MacLaughlin
Yup, that did the trick.
Shane MacLaughlin