Beginner level code required to display a frame using vfw?

629 views Asked by At

I want to be able to display a captured frame from my webcam using the vfw.h library. I have code which captures a single frame and saves it to a Bitmap file. However, I do not know how to then display this Bitmap file within a window. I only know how to display the live video feed within a window. Apologies in advance for the likely obviousness of this question but I have very little experience in this type of programming. Can anyone advise me on how to display the captured frame in another window, for further manipulation, i.e. examples of how to use the relevant macros? I am using C++ in Visual Studio.

ShowWindow(camhwnd,SW_SHOW);
SendMessage(camhwnd,WM_CAP_DRIVER_CONNECT,0,0);
SendMessage(camhwnd, WM_CAP_SET_SCALE, true , 0);
SendMessage(camhwnd, WM_CAP_SET_PREVIEWRATE, 66, 0);
SendMessage(camhwnd, WM_CAP_SET_PREVIEW, true , 0);            
SendMessage(camhwnd, WM_CAP_GRAB_FRAME, 0, 0);
//Grab a Frame
        SendMessage(camhwnd, WM_CAP_GRAB_FRAME, 0, 0);
//Copy the frame we have just grabbed to the clipboard
        SendMessage(camhwnd, WM_CAP_EDIT_COPY,0,0);

//Copy the clipboard image data to a HBITMAP object called hbm
        hdc = BeginPaint(camhwnd, &ps);
        hdcMem = CreateCompatibleDC(hdc);
        if (hdcMem != NULL)
        {
            if (OpenClipboard(camhwnd))
            {
                hbm = (HBITMAP) GetClipboardData(CF_BITMAP);
                SelectObject(hdcMem, hbm);
                GetClientRect(camhwnd, &rc);
                CloseClipboard();
            }
        }
//Save hbm to a .bmp file called Frame.bmp
        PBITMAPINFO pbi = CreateBitmapInfoStruct(hwnd, hbm);
        CreateBMPFile(hwnd, "Frame.bmp", pbi, hbm, hdcMem);

        SendMessage(camhwnd,WM_CAP_DRIVER_CONNECT,0,0);
        SendMessage(camhwnd, WM_CAP_SET_SCALE, true , 0);
        SendMessage(camhwnd, WM_CAP_SET_PREVIEWRATE, 66, 0);
        SendMessage(camhwnd, WM_CAP_SET_PREVIEW, true , 0);
        break;
    }
    }
    break;
}
1

There are 1 answers

0
Roman Ryltsov On

To display a bitmap, you first create a "handled" bitmap object, which results in your holding HBITMAP value which APIs accept for further manipulations, and then you actually display it.

The first step is achieved using CreateBitmap and friends.

On the second, see Displaying HBITMAP.