几何尺寸与公差论坛

 找回密码
 注册
查看: 5796|回复: 0

【转帖】请教:自定义位图类怎样保存成bmp文件

[复制链接]
发表于 2007-6-5 18:10:41 | 显示全部楼层 |阅读模式
我用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;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-12-22 01:23 , Processed in 0.036190 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表