对于一个CBitmap 对象,我们能用GetBitmap() 函数来确定其宽度和高度。
// The variable bitmap is a CBitmap object bitmap变量是一个CBitmap对象
BITMAP bm;
bitmap.GetBitmap( &bm );
bmWidth = bm.bmWidth;
bmHeight = bm.bmHeight;
如果你有一个HBITMAP位图句柄,你可以将它附属于一个CBitmap对象,然后用上面讲到的方法或下面的方法来确定其宽度和高度。
// The variable hBmp is a HBITMAP hBmp变量是一个HBITMAP位图句柄
BITMAP bm;
::GetObject( hBmp, sizeof( bm ), &bm );
bmWidth = bm.bmWidth;
bmHeight = bm.bmHeight;
对于一个文件,你能用下面的代码来实现:
CFile file;
// sBMPFileName is the BMP filename sBMPFileName是一个位图文件名
if( !file.Open( sBMPFileName, CFile::modeRead) )
return ;
BITMAPFILEHEADER bmfHeader;
// Read file header 读文件头信息
if (file.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))
return ;
// File type should be BM 确定文件类型
if (bmfHeader.bfType != ((WORD) (M << 8) | B))
return ;
BITMAPINFOHEADER bmiHeader;
if (file.Read((LPSTR)&bmiHeader, sizeof(bmiHeader)) != sizeof(bmiHeader))
return ;
int bmWidth = bmiHeader.biWidth;
int bmHeight = bmiHeader.biHeight;