如何使用 LoadImage() 读取 BMP 文件

如何使用 LoadImage() 读取 BMP 文件
2010年06月04日
  下面的代码使用来加载一个 DIBSection 作为位图的 [b]LoadImage[/b] API,然后从 DIBSection 的颜色表中创建一个调色板。 是否存在没有颜色表使用半色调调色板:
  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 = (HBITMAP)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 ) rgb[256]; LPLOGPALETTE pLogPal; WORD i; // Create a memory DC and select the DIBSection into it hMemDC = CreateCompatibleDC( NULL ); hOldBitmap = (HBITMAP)SelectObject( hMemDC, *phBitmap ); // Get the DIBSection's color table GetDIBColorTable( hMemDC, 0, 256, rgb ); // Create a palette from the color tabl pLogPal = (LOGPALETTE *)malloc( sizeof(LOGPALETTE) + (256*sizeof(PALETTEENTRY)) ); pLogPal->palVersion = 0x300; pLogPal->palNumEntries = 256; for(i=0;ipalPalEntry.peRed = rgb.rgbRed; pLogPal->palPalEntry.peGreen = rgb.rgbGreen; pLogPal->palPalEntry.peBlue = rgb.rgbBlue; pLogPal->palPalEntry.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; }
  以下代码演示如何使用 [b]LoadBitmapFromBMPFile[/b] 函数:
  case WM_PAINT: { PAINTSTRUCT ps; HBITMAP hBitmap, hOldBitmap; HPALETTE hPalette, hOldPalette; HDC hDC, hMemDC; BITMAP bm; hDC = BeginPaint( hWnd, &ps ); if( LoadBitmapFromBMPFile( szFileName, &hBitmap, &hPalette ) ) { GetObject( hBitmap, sizeof(BITMAP), &bm ); hMemDC = CreateCompatibleDC( hDC ); hOldBitmap = (HBITMAP)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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值