c语言读取位图编程
boolsaveBmp(char*bmpName,unsignedchar*imgBuf,intwidth,intheight,intbiBitCount,RGBQUAD...
bool saveBmp(char *bmpName,unsigned char *imgBuf,int width,int height,int biBitCount,RGBQUAD *PColorTable)
{
if(!imgBuf) return 0;
int colorTablesize=0;
if(biBitCount==8)
colorTablesize=1024;
int lineByte=(width*biBitCount/8+3)/(4*4);
FILE *fp=fopen(bmpName,"wb");
if(fp==0) return 0;
BITMAPFILEHEADER fileHeader;
fileHeader.bfType=0x424d;
fileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+colorTablesize+lineByte *height;
fileHeader.bfReserved1=0;
fileHeader.bfReserved2=0;
fileHeader.bfOffBits=54+colorTablesize;
fwrite(&fileHeader,sizeof(BITMAPFILEHEADER),1,fp);
BITMAPINFOHEADER head;
head.biBitCount=biBitCount;
head.biClrImportant=0;
head.biClrUsed=0;
head.biCompression=0;
head.biHeight=height;
head.biPlanes=1;
head.biSize=40;
head.biSizeImage=lineByte *height;
head.biWidth=width;
head.biXPelsPerMeter=0;
head.biYPelsPerMeter=0;
fwrite(&head,sizeof(BITMAPINFOHEADER),1,fp);
if(biBitCount==8)
fwrite(pColorTable,sizeof(RGBQUAD),256,fp);
fwrite(imgBuf,height*lineByte,1,fp);
fclose(fp);
return 1;
}
这个函数有什么问题吗?为什么我运行得到的图片是图片错误的提示 展开
{
if(!imgBuf) return 0;
int colorTablesize=0;
if(biBitCount==8)
colorTablesize=1024;
int lineByte=(width*biBitCount/8+3)/(4*4);
FILE *fp=fopen(bmpName,"wb");
if(fp==0) return 0;
BITMAPFILEHEADER fileHeader;
fileHeader.bfType=0x424d;
fileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+colorTablesize+lineByte *height;
fileHeader.bfReserved1=0;
fileHeader.bfReserved2=0;
fileHeader.bfOffBits=54+colorTablesize;
fwrite(&fileHeader,sizeof(BITMAPFILEHEADER),1,fp);
BITMAPINFOHEADER head;
head.biBitCount=biBitCount;
head.biClrImportant=0;
head.biClrUsed=0;
head.biCompression=0;
head.biHeight=height;
head.biPlanes=1;
head.biSize=40;
head.biSizeImage=lineByte *height;
head.biWidth=width;
head.biXPelsPerMeter=0;
head.biYPelsPerMeter=0;
fwrite(&head,sizeof(BITMAPINFOHEADER),1,fp);
if(biBitCount==8)
fwrite(pColorTable,sizeof(RGBQUAD),256,fp);
fwrite(imgBuf,height*lineByte,1,fp);
fclose(fp);
return 1;
}
这个函数有什么问题吗?为什么我运行得到的图片是图片错误的提示 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询