How To Use LoadImage() to Read a BMP File

Last reviewed: November 18, 1996
Article ID: Q158898
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) for Windows NT and Win95, version 4.00

SUMMARY

The LoadImage() API can be used to load a bitmap from a BMP file. However, it does not return palette information. This article provides sample code and describes how to retrieve the palette information for the bitmap with LoadImage().

MORE INFORMATION

The following code uses the LoadImage() API to load the bitmap as a DIBSection, and then creates a palette from the DIBSection's color table. If no color table is present, a halftone palette is used:

   BOOL LoadBitmapFromBMPFile( LPTSTR szFileName, HBITMAP *phBitmap,
   HPALETTE *phPalette )
   {
     BITMAP  bm;

     *phBitmap = NULL;
     *phPalette = NULL;

     // Use LoadImage() to get the image loaded into a DIBSection
     *phBitmap = LoadImage( NULL, szFileName, IMAGE_BITMAP, 0, 0,
                 LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE );
     if( *phBitmap == NULL )
       return FALSE;

     // Get the color depth of the DIBSection
     GetObject(*phBitmap, sizeof(BITMAP), &bm );
     // If the DIBSection is 256 color or less, it has a color table
     if( ( bm.bmBitsPixel * bm.bmPlanes ) <= 8 )
     {
       HDC           hMemDC;
       HBITMAP       hOldBitmap;
       RGBQUAD       rgb[256];
       LPLOGPALETTE  pLogPal;
       WORD          i;

       // Create a memory DC and select the DIBSection into it
       hMemDC = CreateCompatibleDC( NULL );
       hOldBitmap = SelectObject( hMemDC, *phBitmap );
       // Get the DIBSection's color table
       GetDIBColorTable( hMemDC, 0, 256, rgb );
       // Create a palette from the color table
       pLogPal = malloc( sizeof(LOGPALETTE) + (256*sizeof(PALETTEENTRY)) );
       pLogPal->palVersion = 0x300;
       pLogPal->palNumEntries = 256;
       for(i=0;i<256;i++)
       {
         pLogPal->palPalEntry[i].peRed = rgb[i].rgbRed;
         pLogPal->palPalEntry[i].peGreen = rgb[i].rgbGreen;
         pLogPal->palPalEntry[i].peBlue = rgb[i].rgbBlue;
         pLogPal->palPalEntry[i].peFlags = 0;
       }
       *phPalette = CreatePalette( pLogPal );
       // Clean up
       free( pLogPal );
       SelectObject( hMemDC, hOldBitmap );
       DeleteDC( hMemDC );
     }
     else   // It has no color table, so use a halftone palette
     {
       HDC    hRefDC;

       hRefDC = GetDC( NULL );
       *phPalette = CreateHalftonePalette( hRefDC );
       ReleaseDC( NULL, hRefDC );
     }
     return TRUE;
   }

The following code demonstrates how to use the LoadBitmapFromBMPFile function:

    case WM_PAINT:
    {
      PAINTSTRUCT   ps;
      HBITMAP       hBitmap, hOldBitmap;
      HPALETTE      hPalette, hOldPalette;
      HDC           hDC, hMemDC;
      BITMAP        bm;

     hDC = BeginPaint( hWnd, &ps );

     if( LoadBitmapFromBMPFile( zFileName, &hBitmap, &hPalette ) )
     {
         GetObject( hBitmap, sizeof(BITMAP), &bm );
         hMemDC = CreateCompatibleDC( hDC );
         hOldBitmap = SelectObject( hMemDC, hBitmap );
         hOldPalette = SelectPalette( hDC, hPalette, FALSE );
         RealizePalette( hDC );

         BitBlt( hDC, 0, 0, bm.bmWidth, bm.bmHeight,
                 hMemDC, 0, 0, SRCCOPY );

         SelectObject( hMemDC, hOldBitmap );
         DeleteObject( hBitmap );
         SelectPalette( hDC, hOldPalette, FALSE );
         DeleteObject( hPalette );
     }
     EndPaint( hWnd, &ps );
   }
   break;


KBCategory: kbgraphic kbcode kbhowto
KBSubcategory: GdiBmp GdiPal
Additional reference words: 4.00 kbdsd BITMAP DDB DIB BMP file LoadImage
LoadBitmap



THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 18, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.