windows bmp图片载入学习

//载入文件 SecureZeroMemory(&lpFileName,sizeof(lpFileName)); //初始化打开文件对话框结构体--打开位图 lpFileName.lStructSize =sizeof(lpFileName); lpFileName.lpstrFile = szOpenFile; lpFileName.lpstrFile[0]= TEXT('/0'); lpFileName.nMaxFile = sizeof(szOpenFile); lpFileName.lpstrFilter = TEXT("Bmp位图文件 *.bmp/0*.bmp/0所有文件 *.*/0*.*/0"); lpFileName.nFilterIndex=1; lpFileName.lpstrFileTitle=NULL; lpFileName.nMaxFileTitle=0; lpFileName.lpstrInitialDir="bmp//"; lpFileName.Flags=OFN_EXPLORER |OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; if(::GetOpenFileName(&lpFileName)==0) ::MessageBox(hwnd,"error",MB_OK,NULL);

 

//读取载入的bmp文件 hFile=::CreateFile(lpFileName.lpstrFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(hFile==INVALID_HANDLE_VALUE) ::MessageBox(hwnd,"hFile error",MB_OK,NULL);

//创建bmp文件内存映射 SetFilePointer(hFile,NULL,0,FILE_BEGIN);//设置文件指针指向开始 HANDLE hFileMap=::CreateFileMapping(hFile,//创建内存映射对象 NULL, PAGE_READONLY, NULL, NULL, NULL); //int err=::GetLastError(); if(!hFileMap) ::MessageBox(hwnd,"hdc error",MB_OK,NULL); //载入整个BMP文件,返回首地址 LPVOID lpBase=::MapViewOfFile(hFileMap,FILE_MAP_READ,0,0,0);

//从内存读取bmp,并把图像放到建立的设备中 HDC hdc=GetDC(hwnd);//获取hdc HDC DCMemory = CreateCompatibleDC(hdc);//创建内存兼容hdc HBITMAP hBitmap=::CreateCompatibleBitmap(hdc,bmpInfo.biWidth,bmpInfo.biHeight); BITMAPINFO *pInfoHeader = (BITMAPINFO*)((BYTE*)lpBase + sizeof(BITMAPFILEHEADER)); ::SelectObject(DCMemory,hBitmap); BYTE* pBits=(BYTE*)lpBase+bmpHeader.bfOffBits; RECT rt; ::GetClientRect(hwnd,&rt);//获取客户区大小 int nRet=::SetDIBitsToDevice(DCMemory,0,0,//把图像放入创建的设备 bmpInfo.biWidth,bmpInfo.biHeight,0,0,0, bmpInfo.biHeight, pBits, pInfoHeader, DIB_RGB_COLORS); //显示图片 ::BitBlt(hdc,0,0,bmpInfo.biWidth,bmpInfo.biHeight,DCMemory,0,0,SRCCOPY);

 

你可能感兴趣的:(windows bmp图片载入学习)