开发者

Trying to make VMR-9 MixerBitmap & GDI work

I'm attempting to overlay a bitmap on some video. I create a bitmap in memory, then I call SelectObject to select it into a memoryDC, so I can perform a DrawText operation on it.

I'm not getting any results on screen at all - can anyone suggest why?

thanks

 HRESULT MyGraph::MixTextOverVMR9(LPCTSTR szText)
{
 // create a bitmap object using GDI, render the text to it accordingly
 // then Sets the bitmap as an alpha bitmap to the VMR9, so that it can be overlayed. 

 HRESULT    hr    = S_OK;
 CBitmap    bmpMem; 
 CFont    font;
 LOGFONT    logicfont;

 CRect    rcText;
 CRect    rcVideo;
 VMR9AlphaBitmap  alphaBmp;

 HWND    hWnd   = this->GetFirstRendererWindow();
 COLORREF   clrText   = RGB(255, 255, 0);
 COLORREF   clrBlack  = RGB(0,0,0);
 HDC     hdcHwnd   = NULL; 
 CDC     dcMem;

 LONG    lWidth;
 LONG    lHeight; 


 if( ! m_spVideoRenderer.p )
  return E_NOINTERFACE;

 if( !m_spWindowlessCtrl.p )
  return E_NOINTERFACE;

 if( ! m_spIMixerBmp9.p )
 {  
  m_spIMixerBmp9 = m_spVideoRenderer;

  if( ! m_spIMixerBmp9.p )
   return E_NOINTERFACE;
 }

 // create the font..
 LPCTSTR sFont = _T("Times New Roman");
 memset(&logicfont, 0, sizeof(LOGFONT));
 logicfont.lfHeight   = 42;
 logicfont.lfWidth   = 20;
 logicfont.lfStrikeOut  = 0;
    logicfont.lfUnderline  =开发者_运维百科 0;
 logicfont.lfItalic   = FALSE;     
 logicfont.lfWeight   = FW_NORMAL;
    logicfont.lfEscapement  = 0; 
 logicfont.lfCharSet   = ANSI_CHARSET;
 logicfont.lfQuality   = ANTIALIASED_QUALITY;
 logicfont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;

 wcscpy_s( &logicfont.lfFaceName[0], wcslen(sFont)*2, sFont );
 font.CreateFontIndirectW(&logicfont);

 // create a compatible memDC from the video window's HDC
 if( hWnd == NULL )
  return E_FAIL;

 hdcHwnd = GetDC(hWnd);
 dcMem = CreateCompatibleDC(hdcHwnd);

 // get the required bitmap metrics from the MediaBuffer 
 if( ! SUCCEEDED(m_spWindowlessCtrl->GetNativeVideoSize(&lWidth, &lHeight, NULL, NULL)) )
  return E_FAIL;

 // create a bitmap for the text
 bmpMem.CreateCompatibleBitmap(dcMem.m_hDC, lWidth, lHeight);

 SelectBitmap (dcMem.m_hDC, bmpMem); 
 SetBkMode  (dcMem.m_hDC, TRANSPARENT);
 SetTextColor (dcMem.m_hDC, clrText);
 SelectFont  (dcMem.m_hDC, font.m_hFont);

 // draw the text
 DrawTextW(dcMem.m_hDC, szText, wcslen(szText), rcText, DT_CALCRECT | DT_NOPREFIX );
 DrawTextW(dcMem.m_hDC, szText, wcslen(szText), rcText, DT_NOPREFIX );

 // Set the alpha bitmap on the VMR9 renderer 
 memset(&alphaBmp, 0, sizeof(VMR9AlphaBitmap)); 

 alphaBmp.rDest.left  = 0;
 alphaBmp.rDest.top  = 0.5;
 alphaBmp.rDest.right = 0.5;
 alphaBmp.rDest.bottom = 1;


 alphaBmp.dwFlags  = VMR9AlphaBitmap_hDC;
 alphaBmp.hdc   = dcMem.m_hDC;
 alphaBmp.pDDS   = NULL;
 alphaBmp.rSrc   = rcText;    // rect to copy from the source image 
 alphaBmp.fAlpha   = 0.5f;     // transparency value (1.0 is opaque, 0.0 is transparent)
 alphaBmp.clrSrcKey  = clrText;
 // alphaBmp.dwFilterMode = MixerPref9_AnisotropicFiltering;


 hr = m_spIMixerBmp9->SetAlphaBitmap(&alphaBmp);

 DeleteDC(hdcHwnd);
 dcMem.DeleteDC();
 bmpMem.DeleteObject();
 font.DeleteObject();

 return hr;
}


I got this one solved, so if anyone else needs to know how it's done...

HRESULT MyGraph::MixTextOverVMR9(LPCTSTR szText)
{
    // create a bitmap object using GDI, render the text to it accordingly
    // then Sets the bitmap as an alpha bitmap to the VMR9, so that it can be overlayed.    

    HRESULT             hr              = S_OK;
    CBitmap             bmpMem; 
    CFont               font;
    LOGFONT             logicfont;

    CRect               rcText;
    CRect               rcVideo;
    VMR9AlphaBitmap     alphaBmp;

    HWND                hWnd            = this->GetFirstRendererWindow();
    COLORREF            clrText         = RGB(255, 255, 0);
    COLORREF            clrBlack        = RGB(0,0,0);
    HDC                 hdcHwnd         = NULL; 
    CDC                 dcMem;

    LONG                lWidth;
    LONG                lHeight;    


    if( ! m_spVideoRenderer.p )
        return E_NOINTERFACE;

    if( !m_spWindowlessCtrl.p )
        return E_NOINTERFACE;

    if( ! m_spIMixerBmp9.p )
    {       
        m_spIMixerBmp9 = m_spVideoRenderer;

        if( ! m_spIMixerBmp9.p )
            return E_NOINTERFACE;
    }

    // create the font..
    LPCTSTR sFont = _T("Impact");
    memset(&logicfont, 0, sizeof(LOGFONT));
    logicfont.lfHeight          = 24;
    //logicfont.lfWidth         = 24;
    logicfont.lfStrikeOut       = 0;
    logicfont.lfUnderline       = 0;
    logicfont.lfItalic          = FALSE;        
    logicfont.lfWeight          = FW_NORMAL;
    logicfont.lfEscapement      = 0; 
    logicfont.lfCharSet         = ANSI_CHARSET;
    logicfont.lfQuality         = ANTIALIASED_QUALITY;
    logicfont.lfPitchAndFamily  = DEFAULT_PITCH | FF_DONTCARE;

    wcscpy_s( &logicfont.lfFaceName[0], wcslen(sFont)*2, sFont );
    font.CreateFontIndirectW(&logicfont);

    // create a compatible memDC from the video window's HDC
    if( hWnd == NULL )
        return E_FAIL;

    hdcHwnd = GetDC(hWnd);
    dcMem   = CreateCompatibleDC(hdcHwnd);

    // get the required bitmap metrics from the MediaBuffer 
    if( ! SUCCEEDED(m_spWindowlessCtrl->GetNativeVideoSize(&lWidth, &lHeight, NULL, NULL)) )
        return E_FAIL;

    // create a bitmap for the text
    bmpMem.CreateCompatibleBitmap(dcMem.m_hDC, lWidth, lHeight);

    SelectBitmap    (dcMem.m_hDC, bmpMem);  
    SetBkColor      (dcMem.m_hDC, clrBlack);    
    SetBkMode       (dcMem.m_hDC, TRANSPARENT);
    SetTextColor    (dcMem.m_hDC, clrText); 
    SelectFont      (dcMem.m_hDC, font.m_hFont);

    // draw the text
    DrawTextW(dcMem.m_hDC, szText, wcslen(szText), rcText, DT_CALCRECT | DT_NOPREFIX );
    DrawTextW(dcMem.m_hDC, szText, wcslen(szText), rcText, DT_NOPREFIX );

    // Set the alpha bitmap on the VMR9 renderer    
    memset(&alphaBmp, 0, sizeof(VMR9AlphaBitmap));  




    // top left
    alphaBmp.rDest.left     = 0.0f + EDGE_BUFFER;
    alphaBmp.rDest.top      = 0.0f + EDGE_BUFFER;
    alphaBmp.rDest.right    = ((float)rcText.right / (float) lWidth) + EDGE_BUFFER;
    alphaBmp.rDest.bottom   = ((float)rcText.bottom / (float)lHeight) + EDGE_BUFFER;

    // top right
    alphaBmp.rDest.left     = 1.0f - ( (float)rcText.right / (float)lWidth)- EDGE_BUFFER;
    alphaBmp.rDest.top      = 0.0f + EDGE_BUFFER;
    alphaBmp.rDest.right    = 1.0f - EDGE_BUFFER;
    alphaBmp.rDest.bottom   = ((float)rcText.bottom / (float)lHeight) + EDGE_BUFFER;

    /*
    // bottom left
    alphaBmp.rDest.left     = 0.0f + EDGE_BUFFER;
    alphaBmp.rDest.top      = 1.0f - ((float)rcText.bottom / (float)lHeight) - EDGE_BUFFER;
    alphaBmp.rDest.right    = ((float)rcText.right / (float) lWidth) + EDGE_BUFFER;
    alphaBmp.rDest.bottom   = 1.0f - EDGE_BUFFER;
    */

    /*
    // bottom right
    alphaBmp.rDest.left     = 1.0f - ( (float)rcText.right / (float)lWidth)- EDGE_BUFFER;
    alphaBmp.rDest.top      = (float)(lHeight - rcText.bottom) / (float)lHeight - EDGE_BUFFER;
    alphaBmp.rDest.right    = 1.0f - EDGE_BUFFER;
    alphaBmp.rDest.bottom   = 1.0f - EDGE_BUFFER;
    */

    alphaBmp.dwFlags        = VMR9AlphaBitmap_hDC | VMRBITMAP_SRCCOLORKEY;
    alphaBmp.hdc            = dcMem.m_hDC;
    alphaBmp.pDDS           = NULL;
    alphaBmp.rSrc           = rcText;               // rect to copy from the source image   
    alphaBmp.fAlpha         = 0.3f;                 // transparency value (1.0 is opaque, 0.0 is transparent)
    alphaBmp.clrSrcKey      = clrBlack; 
    alphaBmp.dwFilterMode   = MixerPref9_PointFiltering;

    hr = m_spIMixerBmp9->SetAlphaBitmap(&alphaBmp);

    DeleteDC(hdcHwnd);
    dcMem.DeleteDC();
    bmpMem.DeleteObject();
    font.DeleteObject();

    return hr;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜