您的位置:首页 > 其它

位图显示

2015-07-04 00:24 218 查看
读位图资源并显示

//1、准备设备dc
CClientDC *pDC = new CClientDC(this);
//2、准备内存dc
CDC memDC;
memDC.CreateCompatibleDC(pDC);
//3、加载资源位图
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP1);
//4、将资源位图选进内存dc
HGDIOBJ hOldBmp = memDC.SelectObject(bmp.m_hObject);
//5、贴图(内存dc贴到设备dc)
CRect rtClient;
GetClientRect(rtClient);
pDC->BitBlt(0,0,rtClient.Width(),rtClient.Height(),&memDC, 0,0,SRCCOPY);
//6、清除GDI对象
memDC.SelectObject(hOldBmp);
memDC.DeleteDC();
bmp.DeleteObject();
if (pDC)
{
delete pDC;
pDC = NULL;
}


读取位图文件并显示

// 打开位图
CFile file;
if (!file.Open("./res/bitmap1.bmp",CFile::modeRead|CFile::typeBinary))
return;
// 读取位图文件头
BITMAPFILEHEADER bmpHeader;
if (file.Read(&bmpHeader,sizeof(bmpHeader)) != sizeof(bmpHeader))
goto end;
if (bmpHeader.bfType != 0x4d42)
goto end;
// 读取位图信息头
BITMAPINFOHEADER bmpInfo;
if (file.Read(&bmpInfo, sizeof(bmpInfo)) != sizeof(bmpInfo))
goto end;
// 获取位图信息
BITMAPINFO *pBitmap = (BITMAPINFO*)new char[sizeof(BITMAPINFOHEADER)];
if (!pBitmap)
goto end;
memcpy(pBitmap,&bmpInfo,sizeof(BITMAPINFOHEADER));
// 获取位图数据
DWORD dwDatas = bmpHeader.bfSize - bmpHeader.bfOffBits;
BYTE *pbmpData = new BYTE[dwDatas];
if (!pbmpData)
{
delete pBitmap;
pBitmap = NULL;
goto end;
}
if (file.Read(pbmpData,dwDatas) != dwDatas)
{
delete pBitmap;
delete pbmpData;
pBitmap = NULL;
pbmpData = NULL;
}

// 贴图
CClientDC *pDC = new CClientDC(this);
// 设置设备环境中位图的拉伸模式(COLORONCOLOR:该模式删除所有消除的像素行,不保留其信息)
pDC->SetStretchBltMode(COLORONCOLOR);
CRect rtClient;
GetClientRect(rtClient);
StretchDIBits(pDC->GetSafeHdc(),0,0,rtClient.Width(),rtClient.Height(),
0,0,bmpInfo.biWidth,bmpInfo.biHeight,pbmpData,pBitmap,DIB_RGB_COLORS,SRCCOPY);
end:
file.Close();
if (pBitmap)
delete pBitmap;
if (pbmpData)
delete pbmpData;
pBitmap = NULL;
pbmpData = NULL;

装入位图文件并显示位图

// 准备设备dc
CClientDC *pDC=new CClientDC(this);
// 加载位图文件
CBitmap Bitmap;
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL,"./res/100.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
// 获得位图对象
Bitmap.Attach(hBitmap);
// 准备内存dc
CDC memDC;
memDC.CreateCompatibleDC(pDC);
memDC.SelectObject(&Bitmap);
// 获得位图信息
BITMAP bmInfo;
Bitmap.GetObject(sizeof(bmInfo),&bmInfo);
// 贴图
pDC->BitBlt(0,0,bmInfo.bmWidth,bmInfo.bmHeight, &memDC, 0,0,SRCCOPY);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: