现在的位置: 首页 > 综合 > 正文

加载BMP文件 并绘制

2013年10月10日 ⁄ 综合 ⁄ 共 2490字 ⁄ 字号 评论关闭
  1. BOOL LoadBitmapFromBMPFile( LPTSTR szFileName, HBITMAP *phBitmap, HPALETTE *phPalette )
  2. {
  3.   BITMAP  bm;
  4.   *phBitmap = NULL;
  5.   *phPalette = NULL;
  6.   // Use LoadImage() to get the image loaded into a DIBSection
  7.   *phBitmap = LoadImage( NULL, szFileName, IMAGE_BITMAP, 0, 0,
  8.     LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE );
  9.   if( *phBitmap == NULL )
  10.     return FALSE;
  11.   // Get the color depth of the DIBSection
  12.   GetObject(*phBitmap, sizeof(BITMAP), &bm );
  13.   // If the DIBSection is 256 color or less, it has a color table
  14.   if( ( bm.bmBitsPixel * bm.bmPlanes ) <= 8 )
  15.   {
  16.     HDC           hMemDC;
  17.     HBITMAP       hOldBitmap;
  18.     RGBQUAD       rgb[256];
  19.     LPLOGPALETTE  pLogPal;
  20.     WORD          i;
  21.     // Create a memory DC and select the DIBSection into it
  22.     hMemDC = CreateCompatibleDC( NULL );
  23.     hOldBitmap = SelectObject( hMemDC, *phBitmap );
  24.     // Get the DIBSection's color table
  25.     GetDIBColorTable( hMemDC, 0, 256, rgb );
  26.     // Create a palette from the color table
  27.     pLogPal = malloc( sizeof(LOGPALETTE) + (256*sizeof(PALETTEENTRY)) );
  28.     pLogPal->palVersion = 0x300;
  29.     pLogPal->palNumEntries = 256;
  30.     for(i=0;i<256;i++)
  31.     {
  32.       pLogPal->palPalEntry[i].peRed = rgb[i].rgbRed;
  33.       pLogPal->palPalEntry[i].peGreen = rgb[i].rgbGreen;
  34.       pLogPal->palPalEntry[i].peBlue = rgb[i].rgbBlue;
  35.       pLogPal->palPalEntry[i].peFlags = 0;
  36.     }
  37.     *phPalette = CreatePalette( pLogPal );
  38.     // Clean up
  39.     free( pLogPal );
  40.     SelectObject( hMemDC, hOldBitmap );
  41.     DeleteDC( hMemDC );
  42.   }
  43.   else   // It has no color table, so use a halftone palette
  44.   {
  45.     HDC    hRefDC;
  46.     hRefDC = GetDC( NULL );
  47.     *phPalette = CreateHalftonePalette( hRefDC );
  48.     ReleaseDC( NULL, hRefDC );
  49.   }
  50.   return TRUE;
  51. }
  52. //The following code demonstrates how to use the LoadBitmapFromBMPFile function: 
  53. case WM_PAINT:
  54. {
  55.   PAINTSTRUCT   ps;
  56.   HBITMAP       hBitmap, hOldBitmap;
  57.   HPALETTE      hPalette, hOldPalette;
  58.   HDC           hDC, hMemDC;
  59.   BITMAP        bm;
  60.   hDC = BeginPaint( hWnd, &ps );
  61.   if( LoadBitmapFromBMPFile( zFileName, &hBitmap, &hPalette ) )
  62.   {
  63.     GetObject( hBitmap, sizeof(BITMAP), &bm );
  64.     hMemDC = CreateCompatibleDC( hDC );
  65.     hOldBitmap = SelectObject( hMemDC, hBitmap );
  66.     hOldPalette = SelectPalette( hDC, hPalette, FALSE );
  67.     RealizePalette( hDC );
  68.     BitBlt( hDC, 0, 0, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY );
  69.     SelectObject( hMemDC, hOldBitmap );
  70.     DeleteObject( hBitmap );
  71.     SelectPalette( hDC, hOldPalette, FALSE );
  72.     DeleteObject( hPalette );
  73.   }
  74.   EndPaint( hWnd, &ps );
  75.   break;
  76. }

抱歉!评论已关闭.