文件操作 - 释放BMP位图资源到指定目录
1 // 释放【BMP文件】到指定的文件夹 2 BOOL ReleaseBMPRes(LPCTSTR szDLLFullPath, UINT uResID, LPCTSTR szResType) 3 { 4 if (uResID<=0 || !szResType) 5 { 6 return FALSE; 7 } 8 9 HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCE(uResID), szResType); 10 if ( NULL == hRsrc ) 11 { 12 return FALSE; 13 } 14 15 DWORD dwSize = SizeofResource(NULL, hRsrc); 16 if ( dwSize <= 0) 17 { 18 return FALSE; 19 } 20 21 HGLOBAL hGlobal = LoadResource(NULL, hRsrc); 22 if ( NULL == hGlobal ) 23 { 24 return FALSE; 25 } 26 27 LPVOID pBuffer = LockResource(hGlobal); 28 if ( NULL ==pBuffer ) 29 { 30 return FALSE; 31 } 32 33 DWORD dwWritten = 0; 34 HANDLE hFile = CreateFile(szDLLFullPath, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 35 if (INVALID_HANDLE_VALUE == hFile ) 36 { 37 return FALSE; 38 } 39 if (szResType = RT_BITMAP) 40 { 41 // 获得位图信息头 42 BITMAPINFOHEADER bmpInfoHeader; 43 CopyMemory(&bmpInfoHeader, pBuffer, sizeof(BITMAPINFOHEADER)); 44 45 //获得位图颜色表中的RGBQUAD结构个数 46 long nColorEntries; 47 switch(bmpInfoHeader.biBitCount) 48 { 49 case 1: 50 nColorEntries = 2; 51 break; 52 case 4: 53 nColorEntries = 16; 54 break; 55 case 8: 56 nColorEntries = 256; 57 break; 58 default: 59 nColorEntries = 0; 60 break; 61 } 62 63 // 根据位图信息头创建位图文件头 64 BITMAPFILEHEADER bmpFileHeader; 65 bmpFileHeader.bfType = 0x4d42; // "BM" 66 bmpFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + dwSize; 67 bmpFileHeader.bfReserved1 = bmpFileHeader.bfReserved2 = 0; 68 bmpFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD); 69 70 // 写入位图文件头 71 WriteFile(hFile, &bmpFileHeader, sizeof(BITMAPFILEHEADER), &dwWritten, NULL); 72 } 73 74 75 if (FALSE == WriteFile(hFile, pBuffer, dwSize, &dwWritten, NULL)) 76 { 77 AfxMessageBox(_T("写入失败!")); 78 return FALSE; 79 } 80 81 CloseHandle(hFile); 82 UnlockResource(hGlobal); 83 FreeResource(hGlobal); 84 return TRUE; 85 }