飞鸽传书 CFile 来读取位图 BMP 文件
2014-05-13 09:47
603 查看
飞鸽传书 在读取位图的时候,需要用到 CFile 来读取位图 BMP 文件,以下是采用CFile打开并显示24位BMP位图的简单示例程序,可实现对位图数据的操作。
可实现对位图数据的操作:
void CTJBmpDlg::OnButtonOpen()
{
// TODO: Add your control notification handler code here
CStdioFile m_File;
// 文件打开对话框
CFileDialog dlg(true,"*.bmp",NULL,NULL,"*.bmp|*.bmp||");
if (dlg.DoModal()==IDOK)
{
// 获得位图文件路径
strImgFilePath = dlg.GetPathName();
Openbmp();
}else
{
return;
}
}
读24位RGB位图:
// 读24位RGB位图
void CTJBmpDlg::Openbmp()
{
CFile hfbmp ;
BITMAPFILEHEADER bmph;
BITMAPINFOHEADER bmpinfoh;
BITMAPINFO bmpInfo;
BYTE* bmpdatabuff=NULL;
CClientDC dc(this);
// 打开BMP文件
hfbmp.Open(strImgFilePath,CFile::modeReadWrite,NULL);
// 读位图文件头
hfbmp.Read(&bmph,sizeof(BITMAPFILEHEADER));
// 设定文件指针
hfbmp.Seek(sizeof(BITMAPFILEHEADER),CFile::begin);
// 读位图信息头
hfbmp.Read(&bmpinfoh,sizeof(BITMAPINFOHEADER));
// 开辟内存空间
bmpdatabuff = new BYTE[bmph.bfSize - sizeof(BITMAPFILEHEADER) - sizeof(BITMAPINFOHEADER)];
// 设定文件指针
hfbmp.Seek(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) , CFile::begin);
// 图RGB位图数据
hfbmp.Read(bmpdatabuff,bmph.bfSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biClrImportant = 0;
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biHeight = bmpinfoh.biHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biSize = 40;
bmpInfo.bmiHeader.biSizeImage = 0;
bmpInfo.bmiHeader.biWidth = bmpinfoh.biWidth;
bmpInfo.bmiHeader.biXPelsPerMeter = 3780;
bmpInfo.bmiHeader.biYPelsPerMeter = 3780;
// 显示位图
StretchDIBits(dc.m_hDC,0,0,bmpinfoh.biWidth,bmpinfoh.biHeight,0,0,bmpinfoh.biWidth,bmpinfoh.biHeight,
bmpdatabuff , &bmpInfo , DIB_RGB_COLORS,SRCCOPY);
// 释放位图数据指针
delete bmpdatabuff;
}
【飞鸽传书网】
来源: 飞鸽传书 CFile 来读取位图 BMP 文件
可实现对位图数据的操作:
void CTJBmpDlg::OnButtonOpen()
{
// TODO: Add your control notification handler code here
CStdioFile m_File;
// 文件打开对话框
CFileDialog dlg(true,"*.bmp",NULL,NULL,"*.bmp|*.bmp||");
if (dlg.DoModal()==IDOK)
{
// 获得位图文件路径
strImgFilePath = dlg.GetPathName();
Openbmp();
}else
{
return;
}
}
读24位RGB位图:
// 读24位RGB位图
void CTJBmpDlg::Openbmp()
{
CFile hfbmp ;
BITMAPFILEHEADER bmph;
BITMAPINFOHEADER bmpinfoh;
BITMAPINFO bmpInfo;
BYTE* bmpdatabuff=NULL;
CClientDC dc(this);
// 打开BMP文件
hfbmp.Open(strImgFilePath,CFile::modeReadWrite,NULL);
// 读位图文件头
hfbmp.Read(&bmph,sizeof(BITMAPFILEHEADER));
// 设定文件指针
hfbmp.Seek(sizeof(BITMAPFILEHEADER),CFile::begin);
// 读位图信息头
hfbmp.Read(&bmpinfoh,sizeof(BITMAPINFOHEADER));
// 开辟内存空间
bmpdatabuff = new BYTE[bmph.bfSize - sizeof(BITMAPFILEHEADER) - sizeof(BITMAPINFOHEADER)];
// 设定文件指针
hfbmp.Seek(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) , CFile::begin);
// 图RGB位图数据
hfbmp.Read(bmpdatabuff,bmph.bfSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biClrImportant = 0;
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biHeight = bmpinfoh.biHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biSize = 40;
bmpInfo.bmiHeader.biSizeImage = 0;
bmpInfo.bmiHeader.biWidth = bmpinfoh.biWidth;
bmpInfo.bmiHeader.biXPelsPerMeter = 3780;
bmpInfo.bmiHeader.biYPelsPerMeter = 3780;
// 显示位图
StretchDIBits(dc.m_hDC,0,0,bmpinfoh.biWidth,bmpinfoh.biHeight,0,0,bmpinfoh.biWidth,bmpinfoh.biHeight,
bmpdatabuff , &bmpInfo , DIB_RGB_COLORS,SRCCOPY);
// 释放位图数据指针
delete bmpdatabuff;
}
【飞鸽传书网】
来源: 飞鸽传书 CFile 来读取位图 BMP 文件
相关文章推荐
- 遍历文件目录及bmp位图信息头的读取
- MFC中怎样读取一个JPG文件或BMP位图文件
- bmp位图文件:读取、写入、24位真彩转8位灰度、灰度图的二值化
- 如何把位图读入内存中并显示?实现从打包文件中读取bmp文件并显示!
- 读取BMP文件中关于位图属性的信息,将一个数值矩阵封装为一个BMP格式的文件
- [转]VC: BMP位图文件解析
- 一名小白读取BMP位图的一点记录
- C++读取与保存bmp图片文件编程实现
- VC++6.0 中BMP位图的读取和显示
- 绘制BMP位图文件
- 飞鸽传书用 CFile::GetStatus 获取文件状态
- BMP头文件格式以及C语言读取头文件
- VC实现BMP位图文件结构及平滑缩放
- BMP文件的读取与显示
- 位图文件(BMP)格式以及Linux下C程序实现
- 飞鸽传书 CFile 大量地对文件进行操作
- 【转】MFC中用CFile读取和写入文件2
- 最简易的BMP文件格式读取和显示示意程序
- JAVA读取BMP图片文件
- 位图文件(BMP)格式分析以及程序实现