如何使用CreateDIBSection显示图像

时间:2017-09-19 06:15:57

标签: c++

我想在窗口上显示图像,将缓存#define PIXEL_VALUE 0.0039215689f void RDP_G_SETBLENDCOLOR(void) { Gfx.BlendColor.R = _SHIFTR(w1, 24, 8) * PIXEL_VALUE; Gfx.BlendColor.G = _SHIFTR(w1, 16, 8) * PIXEL_VALUE; Gfx.BlendColor.B = _SHIFTR(w1, 8, 8) * PIXEL_VALUE; Gfx.BlendColor.A = _SHIFTR(w1, 0, 8) * PIXEL_VALUE; if(OpenGL.Ext_FragmentProgram && (System.Options & BRDP_COMBINER)) { glProgramEnvParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 2, Gfx.BlendColor.R, Gfx.BlendColor.G, Gfx.BlendColor.B, Gfx.BlendColor.A); } } 中的图像数据提供给&CaptureBuffer,CreateDIBForVideo方法从main()上的线程调用。  不知道我哪里出错了它显示黑色窗口。

CreateDIBSection

1 个答案:

答案 0 :(得分:0)

我找到了解决上述问题的方法 由于UCHAR *CaptureBuffer我直接将CaptureBuffer传递给CreateDIBSection,现在我改为将UCHAR *CaptureBuffer复制到char *DisplayBuffer,现在工作正常。

     UCHAR *CaptureBuffer = NULL;
        char *DisplayBuffer = NULL;
        long CaptureSize = NULL;

        void saveImage(unsigned int frame_num, BITMAPINFO &lBmpInfo, std::unique_ptr<BYTE> pBuf, UCHAR* &CaptureBuffer, long &CaptureSize)
            {

                    BITMAPFILEHEADER bmpFileHeader;

                    bmpFileHeader.bfReserved1 = 0;
                    bmpFileHeader.bfReserved2 = 0;
                    bmpFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + lBmpInfo.bmiHeader.biSizeImage;
                    bmpFileHeader.bfType = 'MB';
                    bmpFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

                    CaptureSize = lBmpInfo.bmiHeader.biSizeImage;
                    CaptureBuffer = (UCHAR*)malloc(CaptureSize);

                    memcpy(CaptureBuffer, pBuf.get(), lBmpInfo.bmiHeader.biSizeImage);          
                    //Here am copying CaptureBuffer(uchar) to DisplayBuffer(char)
                    memcpy(DisplayBuffer, CaptureBuffer, CaptureSize);
                    lresult = 0;

            }

    void CreateDIBForVideo()
{
    // ScreenCaptureProcessorGDI is a class it have initialization for capture window screen  
    screenObject = new ScreenCaptureProcessorGDI();
    screenObject->init();

    HDC DisplayDC = CreateDC((LPCWSTR)"DISPLAY", NULL, NULL, NULL);

    BITMAPINFO bmpInfo = { 0 };
    bmpInfo.bmiHeader.biSize= sizeof(BITMAPINFOHEADER);
    bmpInfo.bmiHeader.biWidth = screenObject->lOutputDuplDesc.ModeDesc.Width;
    bmpInfo.bmiHeader.biHeight= screenObject->lOutputDuplDesc.ModeDesc.Height;
    bmpInfo.bmiHeader.biPlanes = 1;
    bmpInfo.bmiHeader.biBitCount = 32;
    bmpInfo.bmiHeader.biCompression = BI_RGB;
    bmpInfo.bmiHeader.biSizeImage = (4 * screenObject->lOutputDuplDesc.ModeDesc.Width * screenObject->lOutputDuplDesc.ModeDesc.Height);
    bmpInfo.bmiHeader.biXPelsPerMeter = 0;
    bmpInfo.bmiHeader.biYPelsPerMeter = 0;
    bmpInfo.bmiHeader.biClrUsed = 0;
    bmpInfo.bmiHeader.biClrImportant = 0;

    CaptureBuffer = NULL;
    HDC pXorDC = CreateCompatibleDC(DisplayDC);
    HBITMAP hXorDib = CreateDIBSection(DisplayDC, &bmpInfo, DIB_RGB_COLORS, (void**)&DisplayBuffer, NULL, 0);

    hXorTemp = (HBITMAP)SelectObject(pXorDC, hXorDib);
    // startGrab this thread capture a windows screen after init() 
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&startGrab, NULL, 0, NULL);
}