Share via


Device-Independent Bitmaps

Windows CE and DirectX® use the device-independent bitmap (DIB) as their native graphics file format. A DIB is a file that contains information describing an image's dimensions, the number of colors it uses, values describing those colors, and data that describes each pixel. Additionally, a DIB contains some lesser-used parameters, like information about file compression, significant colors (if all are not used), and physical dimensions of the image (in case it will end up in print). DIB files usually have the .bmp file extension, although they might occasionally have a .dib extension.

Because the DIB is so pervasive in Windows programming, Windows CE already contains many functions that you can use with DirectX. For example, the following application-defined function combines Windows CE and DirectX functions to load a DIB onto a DirectX surface.

extern C IDirectDrawSurface * DDLoadBitmap(IDirectDraw *pdd, 
    LPCSTR szBitmap, int dx, int dy) 
{ 
    HBITMAP             hbm; 
    BITMAP              bm; 
    DDSURFACEDESC       ddsd; 
    IDirectDrawSurface *pdds; 
 
    // 
    //  This is the Windows CE part. 
    //  Try to load the bitmap as a resource.
    //  If that fails, try it as a file. 
   // 
    hbm = (HBITMAP)LoadImage(
            GetModuleHandle(NULL), szBitmap, 
            IMAGE_BITMAP, dx, dy, LR_CREATEDIBSECTION); 
 
    if (hbm == NULL) 
        hbm = (HBITMAP)LoadImage(
                NULL, szBitmap, IMAGE_BITMAP, dx, dy,
                LR_LOADFROMFILE|LR_CREATEDIBSECTION); 
 
    if (hbm == NULL) 
        return NULL; 
 
    // 
    // Get the size of the bitmap. 
    // 
    GetObject(hbm, sizeof(bm), &bm); 
 
    // 
    // Now, return to DirectX function calls. 
    // Create a DirectDrawSurface for this bitmap. 
    // 
    memset(&ddsd, 0, sizeof(ddsd)); 
    ddsd.dwSize = sizeof(ddsd); 
    ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT |DDSD_WIDTH; 
    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; 
    ddsd.dwWidth = bm.bmWidth; 
    ddsd.dwHeight = bm.bmHeight; 
 
    if (pdd->CreateSurface(&ddsd, &pdds, NULL) != DD_OK) 
        return NULL; 
 
    DDCopyBitmap(pdds, hbm, 0, 0, 0, 0); 
 
    DeleteObject(hbm); 
 
    return pdds; 
} 

For more detailed information about DIB files, see Using Bitmaps.

 Last updated on Thursday, April 08, 2004

© 1992-2003 Microsoft Corporation. All rights reserved.