我用下面的程序可以打开彩色图像,并显示出来。但是如果打开的是灰度图像就无法正常显示,要么图像从中间分成两块,要么最下面会有一条马赛克。另外,我想用GDI+试试,但是从microsoft网站上下载的SDK没有头文件,那位大虾不妨把GUI+的头文件铁出来?BYTE* ReadBMP(int *height, int *width, char *filename)
{ FILE    *ifp;
int     i; BYTE    *temp;

WORD    bfType;         /* bitmap file header */
DWORD   bfSize;
WORD    bfReserved1;
WORD    bfReserved2;
DWORD   bfOffBits;

DWORD   biSize;         /* bitmap info header */
DWORD   biWidth;
DWORD   biHeight;
WORD    biPlanes;
WORD    biBitCount;
DWORD   biCompression;
DWORD   biSizeImage;
DWORD   biXPelsPerMeter;
DWORD   biYPelsPerMeter;
DWORD   biClrUsed;
DWORD   biClrImportant;

int     padBytes;

if((ifp = fopen(filename, "rb"))== NULL) {
printf("\n\n %s does not exit !!!\n", filename);
exit(1);
}

/* read bitmap file header */
temp = (BYTE *)&bfType;
//  for(i = sizeof(bfType)-1; i >= 0; i--) *(temp+i) = getc(ifp);
for(i = 0; i <= sizeof(bfType)-1; i++) *(temp+i) = getc(ifp);

temp = (BYTE *)&bfSize;
//  for(i = sizeof(bfSize)-1; i >= 0; i--) *(temp+i) = getc(ifp);
for(i = 0; i <= sizeof(bfSize)-1; i++) *(temp+i) = getc(ifp);

temp = (BYTE *)&bfReserved1;
//  for(i = sizeof(bfReserved1)-1; i >= 0; i--) *(temp+i) = getc(ifp);
for(i = 0; i <= sizeof(bfReserved1)-1; i++) *(temp+i) = getc(ifp);

temp = (BYTE *)&bfReserved2;
//  for(i = sizeof(bfReserved2)-1; i >= 0; i--) *(temp+i) = getc(ifp);
for(i = 0; i <= sizeof(bfReserved2)-1; i++) *(temp+i) = getc(ifp);

temp = (BYTE *)&bfOffBits;
//  for(i = sizeof(bfOffBits)-1; i >= 0; i--) *(temp+i) = getc(ifp);
for(i = 0; i <= sizeof(bfOffBits)-1; i++) *(temp+i) = getc(ifp);

printf("bfType = %i\n", bfType);
printf("bfSize = %i\n", bfSize);
printf("bfReserved1 = %i\n", bfReserved1);
printf("bfReserved2 = %i\n", bfReserved2);
printf("bfOffBits = %i\n", bfOffBits);


/* read bitmap info header */

temp = (BYTE *)&biSize;
//  for(i = 3; i >= 0; i--) *(temp+i) = getc(ifp);
for(i = 0; i <= 3; i++) *(temp+i) = getc(ifp);
printf("biSize = %i\n", biSize);

temp = (BYTE *)&biWidth;
//  for(i = 3; i >= 0; i--) *(temp+i) = getc(ifp);
for(i = 0; i <= 3; i++) *(temp+i) = getc(ifp);
printf("biWidth = %i\n", biWidth);

temp = (BYTE *)&biHeight;
//  for(i = 3; i >= 0; i--) *(temp+i) = getc(ifp);
for(i = 0; i <= 3; i++) *(temp+i) = getc(ifp);
printf("biHeight  = %i\n", biHeight);

temp = (BYTE *)&biPlanes;
//  for(i = 1; i >= 0; i--) *(temp+i) = getc(ifp);
for(i = 0; i <= 1; i++) *(temp+i) = getc(ifp);
printf("biPlanes  = %i\n", biPlanes);

temp = (BYTE *)&biBitCount;
//  for(i = 1; i >= 0; i--) *(temp+i) = getc(ifp);
for(i = 0; i <= 1; i++) *(temp+i) = getc(ifp);
printf("biBitCount = %i\n", biBitCount);

temp = (BYTE *)&biCompression;
//  for(i = 3; i >= 0; i--) *(temp+i) = getc(ifp);
for(i = 0; i <= 3; i++) *(temp+i) = getc(ifp);
printf("biCompression = %i\n", biCompression);

temp = (BYTE *)&biSizeImage;
//  for(i = 3; i >= 0; i--) *(temp+i) = getc(ifp);
for(i = 0; i <= 3; i++) *(temp+i) = getc(ifp);
printf("biSizeImage = %i\n", biSizeImage);

temp = (BYTE *)&biXPelsPerMeter;
//  for(i = 3; i >= 0; i--) *(temp+i) = getc(ifp);
for(i = 0; i <= 3; i++) *(temp+i) = getc(ifp);
printf("biXPelsPerMeter = %i\n",biXPelsPerMeter);

temp = (BYTE *)&biYPelsPerMeter;
//  for(i = 3; i >= 0; i--) *(temp+i) = getc(ifp);
for(i = 0; i <= 3; i++) *(temp+i) = getc(ifp);
printf("biYPelsPerMeter = %i\n", biYPelsPerMeter);

temp = (BYTE *)&biClrUsed;
//  for(i = 3; i >= 0; i--) *(temp+i) = getc(ifp);
for(i = 0; i <= 3; i++) *(temp+i) = getc(ifp);
printf("biClrUsed = %i\n", biClrUsed);

temp = (BYTE *)&biClrImportant;
//  for(i = 3; i >= 0; i--) *(temp+i) = getc(ifp);
for(i = 0; i <= 3; i++) *(temp+i) = getc(ifp);
printf("biClrImportant = %i\n", biClrImportant);


if(bfType != 0x4D42) {   /* "BM" type  */
printf("%s in not a bitmap file!\n", filename);
exit(1);
}

*width = biWidth;
*height =biHeight;

padBytes = biWidth % 4;

fseek(ifp, (bfOffBits - 54), 1);

fseek(ifp, sizeof(BITMAPFILEHEADER), SEEK_SET); int nLen = bfSize - sizeof(BITMAPFILEHEADER);
BYTE* lpBuf=new BYTE[nLen];
fread(lpBuf, sizeof(BYTE), nLen, ifp);
fclose(ifp);

return lpBuf;
}  /* end of ReadBMP() */
void ShowBMP(HWND hWnd, char *filename) //, TCHAR szFileName[MAX_PATH])
{

// TCHAR szHello[MAX_LOADSTRING];
// LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
int height, width;
BYTE* lpBuf = ReadBMP(&height, &width, filename); BITMAPINFOHEADER* lpbi=(BITMAPINFOHEADER*)lpBuf;
int nW=lpbi->biWidth;
int nH=lpbi->biHeight;
BYTE* lpData=(BYTE*)lpBuf + sizeof(BITMAPINFOHEADER) ;// + sizeof(RGBQUAD);
PAINTSTRUCT ps;
HDC hdc;
// HWND hWnd = GetWindow(hWnd, NULL);
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
// ::StretchDIBits(hdc,0,0,nW,nH,0,0,nW,nH,lpData,(BITMAPINFO*)lpbi,DIB_RGB_COLORS,SRCCOPY); ::StretchDIBits(hdc,250,250,nW,nH,0,0,nW,nH,lpData,(BITMAPINFO*)lpbi, DIB_RGB_COLORS,SRCCOPY);// DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
}