先把它转为ddb
CodeGuru: Converting DDB to DIB
www.codeguru.com/Cpp/G-M/bitmap/article.php/c1765
然后保存
void savebmp(CBitmap& bmp)
{
CPictureHolder picture;
picture.CreateFromBitmap((HBITMAP)bmp, NULL, TRUE);
LPPICTUREDISP pPictureDisp=picture.GetPictureDispatch();
pPictureDisp->Release(); // must be released
IStorage* pStg = 0;
hr = ::StgCreateDocfile(L"c://picttest",
STGM_SHARE_EXCLUSIVE |
STGM_CREATE |
STGM_READWRITE,
0, &pStg);
if(SUCCEEDED(hr))
{
IStream* pStream = 0;
hr = pStg->CreateStream(L"PICTURE",
STGM_SHARE_EXCLUSIVE |
STGM_CREATE |
STGM_READWRITE,
0, 0, &pStream);
if(SUCCEEDED(hr))
{
hr = pPictureDisp->SaveAsFile(pStream,
TRUE, // save mem copy
NULL);
pStream->Release();
}
pStg->Release();
}
pPictureDisp->Release();
}
或者
BOOL WriteDIB( LPTSTR szFile, HANDLE hDIB)
{
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbi;
if (!hDIB)
return FALSE;
CFile file;
if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )
return FALSE;
lpbi = (LPBITMAPINFOHEADER)hDIB;
int nColors = 1 << lpbi->biBitCount;
// Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +
nColors * sizeof(RGBQUAD));
// Write the file header
file.Write( &hdr, sizeof(hdr) );
// Write the DIB header and the bits
file.Write( lpbi, GlobalSize(hDIB) );
return TRUE;
}
/
HANDLE DibFromBitmap (HBITMAP hbm,DWORD biStyle,WORD biBits,HPALETTE hpal,HDC hdc)
{
BITMAP bm;
BITMAPINFOHEADER bi;
BITMAPINFOHEADER FAR *lpbi;
DWORD dwLen;
HANDLE hdib;
HANDLE h;
if (!hbm)
return NULL;
if (hpal == NULL)
hpal = (HPALETTE)GetStockObject(DEFAULT_PALETTE);
GetObject(hbm,sizeof(bm),(LPSTR)&bm);
if (biBits == 0)
biBits = bm.bmPlanes * bm.bmBitsPixel;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = biBits;
bi.biCompression = biStyle;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwLen = bi.biSize + PaletteSize(&bi);
hpal = SelectPalette(hdc,hpal,FALSE);
RealizePalette(hdc);
hdib = GlobalAlloc(GHND,dwLen);
if (!hdib){
SelectPalette(hdc,hpal,FALSE);
::ReleaseDC(NULL,hdc);
return NULL;
}
lpbi = (tagBITMAPINFOHEADER *)(VOID FAR *)GlobalLock(hdib);
*lpbi = bi;
GetDIBits(hdc, hbm, 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
bi = *lpbi;
GlobalUnlock(hdib);
if (bi.biSizeImage == 0){
bi.biSizeImage = WIDTHBYTES((DWORD)bm.bmWidth * biBits) * bm.bmHeight;
if (biStyle != BI_RGB)
bi.biSizeImage = (bi.biSizeImage * 3) / 2;
}
dwLen = bi.biSize + PaletteSize(&bi) + bi.biSizeImage;
if (h = GlobalReAlloc(hdib,dwLen,0))
hdib = h;
else{
GlobalFree(hdib);
hdib = NULL;
SelectPalette(hdc,hpal,FALSE);
return hdib;
}
lpbi = (tagBITMAPINFOHEADER *)(VOID FAR *)GlobalLock(hdib);
if (GetDIBits( hdc,
hbm,
0L,
(DWORD)bi.biHeight,
(LPBYTE)lpbi + (WORD)lpbi->biSize + PaletteSize(lpbi),
(LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS) == 0){
GlobalUnlock(hdib);
hdib = NULL;
SelectPalette(hdc,hpal,FALSE);
return NULL;
}
bi = *lpbi;
GlobalUnlock(hdib);
SelectPalette(hdc,hpal,FALSE);
return hdib;
}
BOOL WriteDIB (LPSTR szFile, HANDLE hdib)
{
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbi;
HFILE fh;
OFSTRUCT of;
if (!hdib)
return FALSE;
fh = OpenFile(szFile, &of, (UINT)OF_CREATE|OF_READWRITE);
if (fh == -1)
return FALSE;
lpbi = (tagBITMAPINFOHEADER *)(VOID FAR *)GlobalLock (hdib);
hdr.bfType = BFT_BITMAP;
hdr.bfSize = GlobalSize (hdib) + SIZEOF_BITMAPFILEHEADER_PACKED;
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits = (DWORD) (SIZEOF_BITMAPFILEHEADER_PACKED + lpbi->biSize +
PaletteSize(lpbi));
#ifdef FIXDWORDALIGNMENT
_lwrite(fh, (LPSTR)&hdr, (UINT)(SIZEOF_BITMAPFILEHEADER_PACKED));
#else
WriteMapFileHeaderandConvertFromDwordAlignToPacked(fh, &hdr);
#endif
_lwrite (fh, (LPSTR)lpbi, GlobalSize (hdib));
GlobalUnlock (hdib);
GlobalFree (hdib);
_lclose(fh);
return TRUE;
}
void WriteMapFileHeaderandConvertFromDwordAlignToPacked(HFILE fh, LPBITMAPFILEHEADER pbf)
{
_lwrite(fh, (LPSTR)&pbf->bfType, (UINT)sizeof (WORD));
_lwrite(fh, (LPSTR)&pbf->bfSize, sizeof(DWORD) * 3);
}