如何从文件加载图片?????
在VC++中如何把一个位图文件加入到 CBitmap 中.
如: LoadBitmap("c:\\a.bmp",...)
非常感谢!
问题点数:50、回复次数:5Top
1 楼Areslee(懒虫易水)回复于 2000-11-10 09:14:00 得分 0
用LoadImage()Top
2 楼milson(ifaq)回复于 2000-11-10 10:05:00 得分 15
HANDLE hBitmap = LoadImage(NULL, sBitmapFileName, IMAGE_BITMAP,0, 0,
LR_LOADFROMFILE);Top
3 楼dingsg(丁丁)回复于 2000-11-10 11:07:00 得分 20
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:
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 ) <= 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 = (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;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:
PAINTSTRUCT ps;
HBITMAP hBitmap, hOldBitmap;
HPALETTE hPalette, hOldPalette;
HDC hDC, hMemDC;
BITMAP bm;
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 );
}
Top
4 楼sun2000(非常可乐)回复于 2000-11-10 11:28:00 得分 0
你可以在msdn中找一下diblook的例程,当然上面的方法也可以。Top
5 楼liulianxi(阿喜)回复于 2000-11-10 12:36:00 得分 15
HBITMAP m_bitmap;
m_bitmap = (HBITMAP)LoadImage(AfxGetInstanceHandle(),
"welcome.bmp",
IMAGE_BITMAP,
0,0,
LR_LOADFROMFILE );
Top




