zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Python之语句与函数
    python语言的特别之处
    kafka消费者客户端
    kafka生产者客户端
    kafka技术分享02--------kafka入门
    kafka技术分享01--------why we study kafka?
    hadoop之hdfs及其工作原理
    hadoop之hdfs------------------FileSystem及其源码分析
    数据结构之红黑树(一)
    mysql中,唯一索引和普通索引应如何选择
  • 原文地址:https://www.cnblogs.com/tiandsp/p/7440819.html
Copyright © 2011-2022 走看看