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

转载于:https://www.cnblogs.com/tiandsp/p/7440819.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值