// write out a BMP file,DirectShow中例子截取的
//m_szSnappedName 要生成的BMP文件名字
//pBuffer BMP数据
HANDLE hf = CreateFile(
m_szSnappedName, GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, NULL, NULL );
if( hf == INVALID_HANDLE_VALUE )
return 0;
// write out the file header
//
BITMAPFILEHEADER bfh;
memset( &bfh, 0, sizeof( bfh ) );
bfh.bfType = 'MB';
bfh.bfSize = sizeof( bfh ) + lBufferSize + sizeof( BITMAPINFOHEADER );
bfh.bfOffBits = sizeof( BITMAPINFOHEADER ) + sizeof( BITMAPFILEHEADER );
DWORD dwWritten = 0;
WriteFile( hf, &bfh, sizeof( bfh ), &dwWritten, NULL );
// and the bitmap format
//
BITMAPINFOHEADER bih;
memset( &bih, 0, sizeof( bih ) );
bih.biSize = sizeof( bih );
bih.biWidth = lWidth;
bih.biHeight = lHeight;
bih.biPlanes = 1;
bih.biBitCount = 24;
dwWritten = 0;
WriteFile( hf, &bih, sizeof( bih ), &dwWritten, NULL );
// and the bits themselves
//
dwWritten = 0;
WriteFile( hf, pBuffer, lBufferSize, &dwWritten, NULL );
CloseHandle( hf );
bFileWritten = TRUE;
// Display the bitmap bits on the dialog's preview window
//显示,下面代码有该函数的定义
DisplayCapturedBits(pBuffer, &bih);
//如果要防止重绘时图片被擦除,可以在OnPaint函数中在调用一次这个函数
BOOL DisplayCapturedBits(BYTE *pBuffer, BITMAPINFOHEADER *pbih)
{
// If we haven't yet snapped a still, return
if (/*!bFileWritten || !pOwner ||*/ !pBuffer)
return FALSE;
// put bits into the preview window with StretchDIBits
//
HWND hwndStill = NULL;
pOwner->GetDlgItem( IDC_STILL, &hwndStill );//IDC_STILL显示图片的控件ID
RECT rc;
::GetWindowRect( hwndStill, &rc );
long lStillWidth = rc.right - rc.left;
long lStillHeight = rc.bottom - rc.top;
HDC hdcStill = GetDC( hwndStill );
PAINTSTRUCT ps;
BeginPaint(hwndStill, &ps);
SetStretchBltMode(hdcStill, COLORONCOLOR);
StretchDIBits(
hdcStill, 0, 0,
lStillWidth, lStillHeight,
0, 0, lWidth, lHeight,
pBuffer,
(BITMAPINFO*) pbih,
DIB_RGB_COLORS,
SRCCOPY );
EndPaint(hwndStill, &ps);
ReleaseDC( hwndStill, hdcStill );
return TRUE;
}