为什么用VC++编写的以下程序不能实现读取一幅BMP图像并保存?求高手指教
#include<afx.h>#include<windows.h>
#include<iostream>
#include<stdlib.h>
#include<math.h>
int nWidth;
int nHeight;
int nColorBits;
int nColor;
int nLen;
int nByteWidth;
BYTE *lpBitmap;
BYTE *lpBits;
void OpenFile(CString FileName);
void SaveFile(CString FileName);
void OpenFile(CString FileName)
{
HANDLE hFile=::CreateFile
(FileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NOR
MAL,NULL);
if(hFile==0)
{
printf("不能打开文件,请重新选择!\n");
return;
}
DWORD WriteNum;
BITMAPFILEHEADER BFH;//位图文件头
ReadFile(hFile,&BFH,14,&WriteNum,NULL);
if((BFH.bfType!='MB')||(WriteNum!=sizeof(BITMAPFILEHEADER)))
{
printf("不是BMP位图文件或数据有误!\n");
return;
}
nLen=GetFileSize(hFile,NULL)-sizeof(BITMAPFILEHEADER);
lpBitmap=new BYTE[nLen];
ReadFile(hFile,lpBitmap,nLen,&WriteNum,NULL);
BITMAPINFOHEADER *BIH=((BITMAPINFOHEADER *)lpBitmap);//位图信息头指
针
nWidth=BIH->biWidth;
nHeight=BIH->biHeight;
nColorBits=BIH->biBitCount;
nByteWidth=(nWidth*nColorBits+31)/32*4;
nColor=(nColorBits>8)?0:(1<<nColorBits);
lpBits=lpBitmap+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*nColor;
CloseHandle(hFile);
}
void SaveFile(CString FileName)
{
HANDLE hFile=::CreateFile
(FileName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_N
ORMAL,NULL);
if(hFile==0)
{
printf("不能创建文件,请重新选择!\n");
return;
}
unsigned long WriteNum;
BITMAPFILEHEADER BFH;
BFH.bfType='MB';
BFH.bfSize=nLen+sizeof(BITMAPFILEHEADER);
BFH.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof
(BITMAPINFOHEADER)+nColor*sizeof(RGBQUAD);
BFH.bfReserved1=BFH.bfReserved2=0;
WriteFile(hFile,&BFH,sizeof(BITMAPFILEHEADER),&WriteNum,NULL);
WriteFile(hFile,(BITMAPINFOHEADER *)lpBitmap,nLen,&WriteNum,NULL);
CloseHandle(hFile);
}
void main(){
char OpenFileName[100];
char SaveFileName[100];
printf("输入待处理的图像路径:\n");
gets(OpenFileName);
printf("保存处理图像结果的路径:\n");
gets(SaveFileName);
OpenFile(OpenFileName);
SaveFile(SaveFileName);
delete []lpBitmap;
}