读书人

c语言24位bmp取得像素指针后为啥画不出

发布时间: 2013-09-05 16:02:06 作者: rapoo

c语言24位bmp获得像素指针后为啥画不出图像来,计算的行字节数乘行数为啥不等于文件头中的size?
BITMAPFILEHEADER bmfh;//BMP文件头变量
BITMAPINFOHEADER bmih;//BMP文件信息变量
int LineByte;//行字节总数
int bmpWidth; //图像宽
int bmpHeight;//图像高
int bmpCount; //图像位数
FILE *fp;
unsigned char *bmpdata;
//找到文件后,打开文件
fp=fopen(m_view_filename,"r");

if(fp==NULL)
{
MessageBox("Can not open the file!","24Open File");
return;
}
fseek(fp,0L,0);
fread(&bmfh,sizeof(bmfh),1,fp);//读取文件头
fread(&bmih,sizeof(bmih),1,fp);//读取文件信息头
bmpHeight=bmih.biHeight;
bmpWidth=bmih.biWidth;
bmpCount=bmih.biBitCount;
LineByte=(bmpWidth*bmpCount/8+3)/4*4;//计算行的字节数
bmpdata=new unsigned char[LineByte*bmpHeight];//动态分配像素区数组大小

//输出图片宽高和位数
CString string;
string.Format("%d %d %d",bmpWidth,bmpHeight,bmpCount);
MessageBox(string);


//寻找像素区读入bmpdata中
fseek(fp,0L,0);
fseek(fp,54L,0);
fread(bmpdata,1,LineByte*bmpHeight,fp);

//输出第一个像素的rgb
CString string1;
string1.Format("%d %d %d",bmpdata[0],bmpdata[1],bmpdata[2]);
MessageBox(string1);

//在屏幕上打点显示图像
for (int i=0; i<bmpHeight;i++)
{
for (int j=0; j<bmpWidth; j++)
{
pDC->SetPixel(j,bmpHeight-i,RGB(bmpdata[i*bmpHeight+j*3],
bmpdata[i*bmpHeight+j*3+1],
bmpdata[i*bmpHeight+j*3+2]));
}
}

fclose(fp);//关闭文件
delete []bmpdata;//释放内存
ReleaseDC(pDC);
[解决办法]
每行数据按4字节对齐,不足补0

读书人网 >C语言

热点推荐