飞鸽传书 CFile 来读取位图 BMP 文件

飞鸽传书 在读取位图的时候,需要用到 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 文件


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值