Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:15:50+00:00 2026-06-17T22:15:50+00:00

How would you draw text on a IMFMediaBuffer object, and write it out to

  • 0

How would you draw text on a IMFMediaBuffer object, and write it out to another IMFMediaBuffer object?

The context is that I’m building an MFT, and initially I tried using Direct2D and Direct3D11 to achieve this, but to no avail.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:15:51+00:00Added an answer on June 17, 2026 at 10:15 pm

    I was able to accomplish this with Windows GDI calls. The MFT I created has RGB32 input/output types, which allows me to copy to/from a Bitmap object. I copy the part of the frame I want to overlay with text into a Windows bitmap I create, draw the text, then copy back to the IMFMediaBuffer. Here’s my code:

    #define BREAK_ON_FAIL(val) { if ( FAILED(hr = (val)) ) { break; } }
    
    HRESULT AddOverlay(IMFSample* pSample)
    {
        HRESULT hr = S_OK;
        IMFMediaBuffer * pBuffer;
        HDC hDC, hMemDC;
        HBITMAP hBitmap, hOldBitmap;
    
        do
        {
            BYTE * pBufferData;
            UINT32 nWidth, nHeight;
            BITMAPV5HEADER bi;
            BYTE * pBitmapData;
            UINT32 nXOffset, nYOffset;
            RECT OverlayRect;
            OverlayRect.left = 0;
            OverlayRect.top = 0;
            OverlayRect.right = 400;
            OverlayRect.bottom = 32;
            DWORD nOverlayWidth = OverlayRect.right - OverlayRect.left;
            DWORD nOverlayHeight = OverlayRect.bottom - OverlayRect.top;
            LONG lFrameStride;
    
            // Get the frame dimensions and stride
            MFGetAttributeSize(m_pInputType, MF_MT_FRAME_SIZE, &nWidth, &nHeight);
            m_pInputType->GetUINT32(MF_MT_DEFAULT_STRIDE, (UINT32*)&lFrameStride))
    
            // Setup offset for the overlay area into the video frame
            nXOffset = (nWidth - nOverlayWidth) / 2;
            nYOffset = nOverlayHeight-1;
    
            // Set up the bitmap header
            ZeroMemory(&bi, sizeof(BITMAPV5HEADER));
            bi.bV5Size          = sizeof(BITMAPV5HEADER);
            bi.bV5Width         = nOverlayWidth;
            // If the stride is negative, the bitmap is bottom-up, which is designated by a negative height
            bi.bV5Height        = (lFrameStride > 0) ? nOverlayHeight : -(LONG)nOverlayHeight;
            bi.bV5Planes        = 1;
            bi.bV5BitCount      = 32;
            bi.bV5Compression   = BI_RGB;
            // The following mask specification specifies a supported 32 BPP
            // alpha format for Windows XP.
            bi.bV5RedMask       = 0x00FF0000;
            bi.bV5GreenMask     = 0x0000FF00;
            bi.bV5BlueMask      = 0x000000FF;
            bi.bV5AlphaMask     = 0xFF000000;
    
            // Create a DIB section with an alpha channel, along with
            // a memory device context
            hDC = GetDC(NULL);
            hBitmap = CreateDIBSection(hDC, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (void**)&pBitmapData, NULL, 0);
            hMemDC = CreateCompatibleDC(hDC);
            ReleaseDC(NULL, hDC);
    
            // Lock the media buffer for our use
            BREAK_ON_FAIL( pSample->GetBufferByIndex(0, &pBuffer) );
            BREAK_ON_FAIL( pBuffer->Lock(&pBufferData, NULL, NULL) );
    
            // Copy the video frame to the bitmap (to support transparency)
            MFCopyImage(pBitmapData, nOverlayWidth*sizeof(RGBQUAD),
                pBufferData + nYOffset*abs(lFrameStride) + nXOffset*sizeof(RGBQUAD), lFrameStride,
                nOverlayWidth*sizeof(RGBQUAD), nOverlayHeight);
    
            // Draw on the bitmap
            hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
            //FillRect(hMemDC, &OverlayRect, WHITE_BRUSH);
            SetTextColor(hMemDC, RGB(255,0,0));
            SetBkMode(hMemDC, TRANSPARENT);
            DrawText(hMemDC, _T("Hello World!"), 12, &OverlayRect, DT_CENTER);
            SelectObject(hMemDC, hOldBitmap);
    
            // Copy the bitmap to the buffer
            MFCopyImage(pBufferData + nYOffset*abs(lFrameStride) + nXOffset*sizeof(RGBQUAD), lFrameStride,
                pBitmapData, nOverlayWidth*sizeof(RGBQUAD),
                nOverlayWidth*sizeof(RGBQUAD), nOverlayHeight);
    
            BREAK_ON_FAIL( pBuffer->Unlock() );
    
        } while(false);
    
        DeleteDC(hMemDC);
        DeleteObject(hBitmap);
        SafeRelease(&pBuffer);
    
        return hr;
    }
    

    References:

    • http://support.microsoft.com/kb/318876
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to draw text on a canvas using the Canvas.drawText call for
I'm using PIL to draw text on an image. How would I wrap a
I'm trying to draw text using Core Text functions, with a line spacing that's
I would like to know how to draw text/label at specific angle using cocos2d.
I would like to draw a line or bar graph using HTML5 canvas. I
I would like to draw a box using SAS/GRAPH with a color gradient (say
I have to design a GUI using Qt. I would like to draw multiple
I'm attempting to draw text to the screen using GLUT in 2d. I want
Hi I have an android canvas that I draw text on with a shadow
Using the imagettftext function from the PHP GD library, one can only draw text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.