用vc++读取bmp格式的图片并显示出来的程序代码
本人初学者,要用VC++做图像增强,可开头就卡住了,希望高手指点,最好再附上一些详细的操作方法。不胜感激!...
本人初学者,要用VC++做图像增强,可开头就卡住了,希望高手指点,最好再附上一些详细的操作方法。不胜感激!
展开
2013-08-15
展开全部
用CDIB类,图像设备无关类。这是里边载入bmp图片的函数,加了这个类后直接调用就行。网上有很多例子,去pudn吧,那里有很多
int CDIB::LoadBmp(LPCSTR filename)
{
//打开文件
CFile file(filename,CFile::modeRead|CFile::shareDenyNone);
WORD bfType;
DWORD
bfSize;
//读入文件头
file.Read(&bfType,sizeof(WORD));
file.Read(&bfSize,sizeof(DWORD));
if(bfSize <= 0)
{
file.Close();
return -1;
}
//是否Bmp文件
if (bfType != (((WORD)'M'<<8) + 'B'))
{
file.Close();
return -1;
}
DWORD
bfOffBits;
//找到位图数据起始偏移并读入
file.Seek(2*sizeof(WORD),CFile::current);
file.Read(&bfOffBits,sizeof(DWORD));
LPVOID DibBuf;
DibBuf = (LPVOID)new char[bfSize];
if(DibBuf == NULL)
{
file.Close();
return -1;
}
//读入位图数据
file.ReadHuge(DibBuf,bfSize);
file.Close();
LPBITMAPINFO pBMI;
int size = bfOffBits - 14;
pBMI = (LPBITMAPINFO)new char[sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)];
//生成信息头
memcpy(pBMI,DibBuf,size);
LPVOID lpBits = (LPVOID)((LPSTR)DibBuf + size);
//创建位图
CreateDIBIndirect(pBMI, lpBits);
delete pBMI;
delete DibBuf;
return 1;
}
int CDIB::LoadBmp(LPCSTR filename)
{
//打开文件
CFile file(filename,CFile::modeRead|CFile::shareDenyNone);
WORD bfType;
DWORD
bfSize;
//读入文件头
file.Read(&bfType,sizeof(WORD));
file.Read(&bfSize,sizeof(DWORD));
if(bfSize <= 0)
{
file.Close();
return -1;
}
//是否Bmp文件
if (bfType != (((WORD)'M'<<8) + 'B'))
{
file.Close();
return -1;
}
DWORD
bfOffBits;
//找到位图数据起始偏移并读入
file.Seek(2*sizeof(WORD),CFile::current);
file.Read(&bfOffBits,sizeof(DWORD));
LPVOID DibBuf;
DibBuf = (LPVOID)new char[bfSize];
if(DibBuf == NULL)
{
file.Close();
return -1;
}
//读入位图数据
file.ReadHuge(DibBuf,bfSize);
file.Close();
LPBITMAPINFO pBMI;
int size = bfOffBits - 14;
pBMI = (LPBITMAPINFO)new char[sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)];
//生成信息头
memcpy(pBMI,DibBuf,size);
LPVOID lpBits = (LPVOID)((LPSTR)DibBuf + size);
//创建位图
CreateDIBIndirect(pBMI, lpBits);
delete pBMI;
delete DibBuf;
return 1;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询