opengl截图

复制代码
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
    UINT  num = 0;          // number of image encoders
    UINT  size = 0;         // size of the image encoder array in bytes

    ImageCodecInfo* pImageCodecInfo = NULL;

    GetImageEncodersSize(&num, &size);
    if(size == 0)
        return -1;  // Failure

    pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
    if(pImageCodecInfo == NULL)
        return -1;  // Failure

    GetImageEncoders(num, size, pImageCodecInfo);

    for(UINT j = 0; j < num; ++j)
    {
        if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
        {
            *pClsid = pImageCodecInfo[j].Clsid;
            free(pImageCodecInfo);
            return j;  // Success
        }    
    }

    free(pImageCodecInfo);
    return -1;  // Failure
}

bool CaptureScreenShot(
    int nWidth, 
    int nHeight, 
    const std::wstring& szDestFile,
    const std::wstring& szEncoderString)
{
    UINT *pixels=new UINT[nWidth * nHeight];
    memset(pixels, 0, sizeof(UINT)*nWidth*nHeight);

    glFlush(); glFinish();

    glReadPixels(0,0,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(0, 0, nWidth, nHeight);

        BitmapData bitmapData;
        memset( &bitmapData, 0, sizeof(bitmapData));
        DestBmp.LockBits( 
            &rect1, 
            ImageLockModeRead,
            PixelFormat32bppARGB,
            &bitmapData );

        int nStride1 = bitmapData.Stride;
        if( nStride1 < 0 )
            nStride1 = -nStride1;

        UINT* DestPixels = (UINT*)bitmapData.Scan0;

        if( !DestPixels )
        {
            delete [] pixels;
            return false;
        }

        for(UINT row = 0; row < bitmapData.Height; ++row)
        {
            for(UINT col = 0; col < bitmapData.Width; ++col)
            {
                DestPixels[row * nStride1 / 4 + col] = pixels[row * nWidth + col];
            }
        }

        DestBmp.UnlockBits( 
            &bitmapData );

        delete [] pixels;
        pixels = NULL;

        DestBmp.RotateFlip( RotateNoneFlipY );

        CLSID Clsid;
        int result = GetEncoderClsid(szEncoderString.c_str(), &Clsid);

        if( result < 0 )
            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, 0, sizeof(FullPath) );
    std::wstring szExePath;
    if (::GetModuleFileNameW( NULL, FullPath, sizeof(wchar_t)*MAX_PATH))
    {
        szExePath = FullPath;

        int pos = szExePath.rfind( L'\\' );

        if( -1 != pos )
        {
            szExePath = szExePath.substr(0,pos+1);
        }
    }

    std::wstring szDestFile = szExePath;
    szDestFile += L"somepic.png";

    RECT rect;
    memset(&rect,0,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

posted @ 2017-08-27 17:04  Dsp Tian  阅读(1547)  评论(0编辑  收藏  举报