尝试制作 VMR-9 MixerBitmap & GDI工作

发布于 2024-09-04 22:36:06 字数 3048 浏览 2 评论 0原文

我正在尝试在某些视频上叠加位图。我在内存中创建一个位图,然后调用 SelectObject 将其选择到内存 DC 中,这样我就可以对其执行 DrawText 操作。

我在屏幕上根本没有得到任何结果 - 谁能告诉我为什么?

谢谢

 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'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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莳間冲淡了誓言ζ 2024-09-11 22:36:06

我已经解决了这个问题,所以如果其他人需要知道它是如何完成的......

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文