Go Premium for a chance to win a PS4. Enter to Win


Video for Windows --> Drawing a Line on the Capture Window

Posted on 2004-04-09
Medium Priority
Last Modified: 2007-12-19
I'd like to ask if anyone  have already done this trick.
I'd like to draw a line on the capture window for my program.
I'd like to draw a small cross on the center of the capture
If I can use a function can anybody share this information(function),
if not can anybody share the procedure on how to do this trick.

Thank you ing advance.

Question by:colossus_21
  • 3
LVL 48

Expert Comment

ID: 10789937
I have the project which does exactly the same trick. This is MFC application, you you don't use MFC, convert this to API. Appropriate code fragments:

BOOL CVideoCaptureDlg::InitCapture()
    m_hCaptureWnd = capCreateCaptureWindow(

        // ... show available drivers in listbox

BOOL CVideoCaptureDlg::Connect()
    int n = m_list_drivers.GetCurSel();  // driver number

    if ( n < 0 )
        MessageBox("Capture driver is not selected");
        return FALSE;

    if ( ! capDriverConnect(m_hCaptureWnd, n) )
        MessageBox("capDriverConnect failed");
        return FALSE;

    CAPSTATUS CapStatus;

    if ( ! capGetStatus(m_hCaptureWnd, &CapStatus, sizeof (CAPSTATUS)) )
        MessageBox("capGetStatus failed");
        return FALSE;

    ::MoveWindow(m_hCaptureWnd, 0, 0,
                 CapStatus.uiImageWidth, CapStatus.uiImageHeight, FALSE);
    ::ShowWindow(m_hCaptureWnd, SW_SHOW);

    if ( ! capDriverGetCaps(m_hCaptureWnd, &m_DriverCaps, sizeof(CAPDRIVERCAPS)) )
        MessageBox("capDriverGetCaps failed");
        return FALSE;

    if ( ! capSetCallbackOnError(m_hCaptureWnd, ErrorCallback) )
        MessageBox("capSetCallbackOnError failed");
        return FALSE;

    // !!! set frame callback - this is what you need
    if ( ! capSetCallbackOnFrame(m_hCaptureWnd, FrameCallback) )
        MessageBox("capSetCallbackOnFrame failed");
        return FALSE;

    return TRUE;

// This function is used to set RGB video format programmatically -
// I need this format to draw in the video frame
BOOL CVideoCaptureDlg::SetVideoFormatRGB()
    DWORD size = capGetVideoFormatSize(m_hCaptureWnd);

    if ( size <= 0 )
        AddString("capGetVideoFormatSize failed");
        return FALSE;

    void* pBuffer = new BYTE[size];
    BITMAPINFO* pBitmapInfo = (BITMAPINFO*) pBuffer;

    if ( capGetVideoFormat(m_hCaptureWnd, pBuffer, size) <= 0 )
        delete[] pBuffer;
        AddString("capGetVideoFormat failed");
        return FALSE;

    pBitmapInfo->bmiHeader.biBitCount = 24;
    pBitmapInfo->bmiHeader.biCompression = 0;
    pBitmapInfo->bmiHeader.biSizeImage = pBitmapInfo->bmiHeader.biWidth * pBitmapInfo->bmiHeader.biHeight * 3;

    if ( ! capSetVideoFormat(m_hCaptureWnd, pBitmapInfo, size) )
        delete[] pBuffer;
        AddString("capSetVideoFormat failed");
        return FALSE;

    // keep results in global variables to be available in callback functions
    g_nImageWidth = pBitmapInfo->bmiHeader.biWidth;
    g_nImageHeight = pBitmapInfo->bmiHeader.biHeight;
    g_nBitCount = pBitmapInfo->bmiHeader.biBitCount;
    g_nBitsPerPixel = pBitmapInfo->bmiHeader.biBitCount;

    delete[] pBuffer;
    return TRUE;

This is general stuff, I suppose you have something like tiis in your project. Important thing is capSetCallbackOnFrame and SetVideoFormatRGB which sets well-known RGB video format.

#define LINE_LEN 10

    if ( g_nBitCount != 24 )   // only RGB format !
        return 0;

    if ( ! g_hFrameBitmap )

    if ( ! g_hFrameBitmap )   // failed
        return 0;

    HPEN hOldPen = (HPEN)SelectObject(g_hFrameDC, g_hWhitePen);       // global pen handle created in initialization code
    HBRUSH hOldBrush = (HBRUSH)SelectObject(g_hFrameDC, g_hBrush);// global brush handle created in initialization code

    // copy video frame to bitmap bits
    SetBitmapBits(g_hFrameBitmap, g_nImageWidth*g_nImageHeight*3, lpVHdr->lpData);

    // draw something
    MoveToEx(g_hFrameDC, g_nImageWidth/2 - LINE_LEN, g_nImageHeight/2, NULL);
    LineTo(g_hFrameDC, g_nImageWidth/2 + LINE_LEN + 1, g_nImageHeight/2);
    MoveToEx(g_hFrameDC, g_nImageWidth/2, g_nImageHeight/2 - LINE_LEN, NULL);
    LineTo(g_hFrameDC, g_nImageWidth/2, g_nImageHeight/2 + LINE_LEN + 1);

    SelectObject(g_hFrameDC, g_hBlackPen);

    MoveToEx(g_hFrameDC, g_nImageWidth/2 - LINE_LEN, g_nImageHeight/2 - 1, NULL);
    LineTo(g_hFrameDC, g_nImageWidth/2 + LINE_LEN + 1, g_nImageHeight/2 - 1);
    MoveToEx(g_hFrameDC, g_nImageWidth/2 - 1, g_nImageHeight/2 - LINE_LEN, NULL);
    LineTo(g_hFrameDC, g_nImageWidth/2 - 1, g_nImageHeight/2 + LINE_LEN + 1);

    SelectObject(g_hFrameDC, hOldBrush);
    SelectObject(g_hFrameDC, hOldPen);

    // copy bitmap bits back to video frame    
    GetBitmapBits(g_hFrameBitmap, g_nImageWidth*g_nImageHeight*3, lpVHdr->lpData);

    return 0;

#define WIDTHBYTES(bits) ((((bits) + 31) / 32) * 4)

void CreateGraphicObjects(HWND hWnd)
    HDC hDC = ::GetDC(hWnd);
    g_hFrameDC = CreateCompatibleDC(hDC);
    ::ReleaseDC(hWnd, hDC);


    lpbi = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER) + (256 * sizeof(RGBQUAD))];
    lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    lpbi->bmiHeader.biWidth = g_nImageWidth;
    lpbi->bmiHeader.biHeight = g_nImageHeight;
    lpbi->bmiHeader.biPlanes = 1;
    lpbi->bmiHeader.biBitCount = 24;
    lpbi->bmiHeader.biCompression = BI_RGB;
    lpbi->bmiHeader.biSizeImage = WIDTHBYTES((DWORD)g_nImageWidth * 8) * g_nImageHeight;
    lpbi->bmiHeader.biXPelsPerMeter = 0;
    lpbi->bmiHeader.biYPelsPerMeter = 0;
    lpbi->bmiHeader.biClrUsed = 0;
    lpbi->bmiHeader.biClrImportant = 0;

    BYTE* pBits;

    g_hFrameBitmap = CreateDIBSection(
        NULL,                       // DC - we don't need it here
        lpbi,                       // BITMAPINFOHEADER pointer
        DIB_RGB_COLORS,             // palette is in BITMAPINFO, contains RGBQUAD entries (doesn't matter for RGB)
        (void **)&pBits,             // output pointer to bitmap bits
        0 );

    delete[] lpbi;

    SelectObject(g_hFrameDC, (HBITMAP)g_hFrameBitmap);

g_hFrameBitmap is 24 bpp DIB selected in memory DC g_hFrameDC. Each frame is copied to this bitmap bits, after this lines (and some other stuff in my project) are drawn in this DC, and bitmap bits are copied back to video frame.

Expert Comment

ID: 10789955

if you can get the window handles of your programs windows, but not others......

You can call "GetDC" function to get the "display device context" handle for the client area of that window.
And can draw anything...

check this link..

To draw line you can use "Line Functions"

--> MoveToEx
--> LineTo

check this link....


If you are using win32 i.e WinMain()
Then in the WM_PAINT Message of "Window Procedure" you have to call line functions.

LVL 48

Accepted Solution

AlexFM earned 1000 total points
ID: 10790673
By the way, you can draw line manually setting pixel values in lpVHdr->lpData array. Drawing horizontal line may be done using memset, drawing of vertical line may be done in loop calculating every pixel address.

    // change pixels here directly setting them using lpVHdr->lpData array.
    // Each pixel gets 3 bytes (assuming video format is RGB).
    // If you know image size, you have enough information to do this.

    // Example:
    memset(lpVHdr->lpData, 0, 300);   // first 100 pixels in the video capture imahe will be black

Which approach to use - it depends on complexity of graphics overlay you want to draw. Simple line may be drawn by direct pixel access. Complicated overlay is better to draw using device context.
LVL 48

Expert Comment

ID: 10992840
I think my answer is OK.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question