- Код: Выделить всё
HRESULT
SaveSnapshotToBMP(
LPCWSTR pwcPath,
IVMRWindowlessControl* pWindowless
)
{
HRESULT hr = S_OK;
LPBYTE lpBuffer = NULL;
BITMAPINFOHEADER* pbih = NULL;
LPBYTE lpDib = NULL;
BITMAPFILEHEADER FileHeader;
HANDLE hFile = INVALID_HANDLE_VALUE;
const int iSZFileHeader = 14;
DWORD iWritten = 0L;
DWORD dwSize = 0L;
// check input params here
// _HR is defined as if( FAILED( hr )) break;
Do
{
_HR( hr = pWindowless->GetCurrentImage( &lpBuffer ));
// lpBuffer is simply { BITMAPINFOHEADER + DIB bits }
pbih = (BITMAPINFOHEADER*)lpBuffer;
lpDib = lpBuffer + pbih->biSizeImage;
// fill in the file header
FileHeader.bfType = static_cast<WORD>(L'B' | (L'M' << 8));
FileHeader.bfSize = pbih->biSize + iSZFileHeader + pbih->biSizeImage;
FileHeader.bfReserved1 = 0;
FileHeader.bfReserved2 = 0;
FileHeader.bfOffBits = iSZFileHeader + pbih->biSize;
// create a file
hFile = CreateFile( pwcPath,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if( INVALID_HANDLE_VALUE == hFile )
{
_HR( hr = HRESULT_FROM_WIN32(GetLastError()) );
}
// write to a BMP file: FILE HEADER + BITMAP HEADER + DIB bits
// write file header
if( !WriteFile( hFile,
static_cast<LPVOID>(&FileHeader),
iSZFileHeader,
&iWritten,
NULL) )
{
_HR( hr = HRESULT_FROM_WIN32(GetLastError()));
}
// write bitmapinfoheader
if( !WriteFile( hFile,
static_cast<LPVOID>(pbih),
pbih->biSize,
&iWritten,
NULL) )
{
_HR( hr = HRESULT_FROM_WIN32(GetLastError()));
}
// write dib section
dwSize = pbih->biSizeImage;
if( !dwSize && (pbih->biCompression == BI_RGB) )
{
DWORD h = ( pbih->biHeight > 0 ) ? pbih->biHeight : -(pbih->biHeight);
dwSize = h * pbih->biWidth * pbih->biBitCount / 8;
}
if( !WriteFile( hFile, static_cast<LPVOID>(pDib), dwSize, &iWritten, NULL) )
{
_HR( hr = HRESULT_FROM_WIN32(GetLastError()));
}
}while( FALSE );
if( INVALID_HANDLE_VALUE != hFile )
{
CloseHandle( hFile );
}
// do not forget to release the buffer
if( lpBuffer )
{
::CoTastMemFree( lpBuffer );
}
return hr;
}
PS. Родина Вас не забудет!