您的位置:首页 > 其它

飞鸽传书 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 文件


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: