
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) { UINT num = ; // number of image encoders UINT size = ; // size of the image encoder array in bytes ImageCodecInfo* pImageCodecInfo = NULL; GetImageEncodersSize(&num, &size); ) ; // Failure pImageCodecInfo = (ImageCodecInfo*)(malloc(size)); if(pImageCodecInfo == NULL) ; // Failure GetImageEncoders(num, size, pImageCodecInfo); ; j < num; ++j) { ) { *pClsid = pImageCodecInfo[j].Clsid; free(pImageCodecInfo); return j; // Success } } free(pImageCodecInfo); ; // Failure } bool CaptureScreenShot( int nWidth, int nHeight, const std::wstring& szDestFile, const std::wstring& szEncoderString) { UINT *pixels=new UINT[nWidth * nHeight]; memset(pixels, , sizeof(UINT)*nWidth*nHeight); glFlush(); glFinish(); glReadPixels(,,nWidth,nHeight,GL_BGRA_EXT,GL_UNSIGNED_BYTE,pixels); if(NULL==pixels) return false; // Initialize GDI+ GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); { // Create the dest image Bitmap DestBmp(nWidth,nHeight,PixelFormat32bppARGB); Rect rect1(, , nWidth, nHeight); BitmapData bitmapData; memset( &bitmapData, , sizeof(bitmapData)); DestBmp.LockBits( &rect1, ImageLockModeRead, PixelFormat32bppARGB, &bitmapData ); int nStride1 = bitmapData.Stride; ) nStride1 = -nStride1; UINT* DestPixels = (UINT*)bitmapData.Scan0; if( !DestPixels ) { delete [] pixels; return false; } ; row < bitmapData.Height; ++row) { ; col < bitmapData.Width; ++col) { DestPixels[row * nStride1 / + col] = pixels[row * nWidth + col]; } } DestBmp.UnlockBits( &bitmapData ); delete [] pixels; pixels = NULL; DestBmp.RotateFlip( RotateNoneFlipY ); CLSID Clsid; int result = GetEncoderClsid(szEncoderString.c_str(), &Clsid); ) return false; Status status = DestBmp.Save( szDestFile.c_str(), &Clsid ); } // Shutdown GDI+ GdiplusShutdown(gdiplusToken); return true; } void saveImage() { wchar_t FullPath[MAX_PATH]; memset( FullPath, , sizeof(FullPath) ); std::wstring szExePath; if (::GetModuleFileNameW( NULL, FullPath, sizeof(wchar_t)*MAX_PATH)) { szExePath = FullPath; int pos = szExePath.rfind( L'\\' ); != pos ) { szExePath = szExePath.substr(,pos+); } } std::wstring szDestFile = szExePath; szDestFile += L"somepic.png"; RECT rect; memset(&rect,,sizeof(rect)); HWND g_hWnd=GetFocus(); GetClientRect(g_hWnd,&rect); CaptureScreenShot( rect.right, rect.bottom, szDestFile, L"image/png"); }
从这里看的http://forums.codeguru.com/showthread.php?446641-How-can-I-output-an-image-generated-with-openGL-to-an-image-file-such-as-jpg