vc打开bmp格式图片
我建的是基于窗口的mfc工程 无法显示图片 一直显示内存不足 求高手解决FILE *fp;
RGBQUAD * ColorTable;
BYTE * line_buf;
int line_Width;
fp=fopen("c:\\tp\\tt.bmp","rb");
if (bf.bfType!=0x4D42)
{
fclose(fp);
}
fread(&bi,40,1,fp);
line_Width=((bi.biWidth + 3)/4)*4;
line_buf=(BYTE*)new BYTE[line_Width];
int bufsize;
bufsize=bi.biWidth*bi.biHeight;
r=(BYTE*)new BYTE[bufsize];
g=(BYTE*)new BYTE[bufsize];
b=(BYTE*)new BYTE[bufsize];
ColorTable=new RGBQUAD[256];
fread(ColorTable,sizeof(ColorTable),256,fp);
int m,n,i,l=0;
for(m=0;m<bi.biHeight;m++)
{
fread(line_buf,line_Width,1,fp);
for(n=0;n<bi.biWidth;n++)
{
i=line_buf[n];
r[l]=ColorTable[i].rgbBlue;
g[l]=ColorTable[i].rgbGreen;
b[l]=ColorTable[i].rgbRed;
l++;
}
}
fclose(fp);
nMarkIsRead = 1;
if (nMarkIsRead == 1)
{
CPaintDC dc(this);
int nColorIndex, i, j, nWidth;
BYTE btBlue,btGreen,btRed;
COLORREF color;
if (bi.biBitCount == 8)
{
nWidth = ((bi.biWidth + 3) / 4) * 4;
}
else if (bi.biBitCount == 24)
{
nWidth = ((bi.biWidth * 3 + 3) / 4) * 4;
}
else
{
MessageBox("Wrong BitCount");
return ;
}
if (bi.biBitCount == 8)
{
for (j = 0; j < bi.biHeight; j++)
{
for(i = 0; i < bi.biWidth; i ++)
{
nColorIndex = j * bi.biWidth + i;
btBlue = b[nColorIndex];
btGreen = g[nColorIndex];
btRed = r[nColorIndex];
color = RGB(btRed, btGreen, btBlue);
dc.SetPixel(i, bi.biHeight - 1 - j, color);
}
}
}
}
在头文件中建立的全局变量
public:
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;
int nMarkIsRead;
BYTE *r,*g,*b;