|
我用COLORREFP定义成一维数组,从bmp中读取颜色值,修改后,请问将其如何保存到文件中去?
---------------------------------------------------------------
http://www.vckbase.com/code/listcode.asp?mclsid=7&sclsid=705
---------------------------------------------------------------
SaveFile(HDC hdc, HBITMAP hBitmap, CString strFileName)
{
HDC hDC;
int iBits;
WORD wBitCount;
DWORD dwPaletteSize=0;
DWORD dwBmBitsSize, dwDIBSize, dwWritten;
BITMAP Bitmap;
BITMAPFILEHEADER bmfHdr;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
HANDLE hFile, hDib, hPal,hOldPal=NULL;
try
{
int ilength = strFileName.Find(":\\");
ilength = strFileName.Find("\\",ilength+2);
while(ilength !=-1)
{
CString strDirectory =strFileName.Left(ilength);
CFileFind FileFind;
if(!FileFind.FindFile(strDirectory,0))
if(!CreateDirectory(strDirectory,NULL))
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("フォルダクリエートに失敗しました。", NULL, MB_OK + MB_ICONSTOP + MB_SYSTEMMODAL);
}
else
return FALSE;
ilength = strFileName.Find("\\",ilength+1);
}
hDC = CreateDC("DISPLAY",NULL,NULL,NULL);
iBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
DeleteDC(hDC);
if (iBits <= 1)
wBitCount = 1;
else if (iBits <= 4)
wBitCount = 4;
else if (iBits <= 8)
wBitCount = 8;
else if (iBits <= 24)
wBitCount = 24;
else if (iBits <= 32)
wBitCount = 32;
if (wBitCount <= 8)
dwPaletteSize = (1 << wBitCount) * sizeof(RGBQUAD);
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = Bitmap.bmWidth;
bi.biHeight = Bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wBitCount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwBmBitsSize = ((Bitmap.bmWidth *wBitCount+31)/32)* 4*Bitmap.bmHeight ;
hDib = GlobalAlloc(GHND,dwBmBitsSize+dwPaletteSize+sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
*lpbi = bi;
hPal = GetStockObject(DEFAULT_PALETTE);
// CClientDC dc(this);
GetDIBits(hdc, hBitmap, 0, (UINT) Bitmap.bmHeight,(LPSTR)lpbi + sizeof(BITMAPINFOHEADER)
+dwPaletteSize,(BITMAPINFO *)lpbi, DIB_RGB_COLORS);
hFile = CreateFile(strFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
bmfHdr.bfType = 0x4D42;
dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+ dwPaletteSize
+ dwBmBitsSize;
bmfHdr.bfSize = dwDIBSize;
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER)+(DWORD)sizeof(BITMAPINFOHEADER)
+dwPaletteSize;
WriteFile(hFile, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
WriteFile(hFile, (LPSTR)lpbi, dwDIBSize,&dwWritten, NULL);
GlobalUnlock(hDib);
GlobalFree(hDib);
CloseHandle(hFile);
}
catch(...)
{
return FALSE;
}
return TRUE;
} |
|