如何打开灰度图像(BMP格式),并显示出来

bluelion 2002-07-18 09:29:50
我用下面的程序可以打开彩色图像,并显示出来。但是如果打开的是灰度图像就无法正常显示,要么图像从中间分成两块,要么最下面会有一条马赛克。另外,我想用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);
}
...全文
281 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
akiy 2002-09-11
  • 打赏
  • 举报
回复
然后用
CBitmap bmp;
bmp.attach(hbp)
BITMAP m_bp;
bmp.GetObject(m_bp);
m_bp->biwidth;/宽度
m_bp-> biHeight;//高度
。/。。。
akiy 2002-09-11
  • 打赏
  • 举报
回复
这样写还不给人累死了,LoadImage()

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧