Directshow 如何在视频中插入水印Logo图片

下面介绍如何在视频文件中加入自己的水印图片及LOGO


typedef struct _VMRALPHABITMAP {   

 DWORD dwFlags;                                                                                                          //指明从哪儿获取位图

HDC hdc;                                                                                                                         //指定的设备上下文DC。如果指定为NULL,则位图是从一个DirectDraw surface上获取。

LPDIRECTDRAWSURFACE7 pDDS;                                                                            // 指向 DirectDraw surface的一个指针。如果指定为NULL则,位图是从GDI DC中获取。

RECT rSrc;                                                                                                                     //指明源矩形

NORMALIZEDRECT rDest;                                                                                            //指定目标矩形

 FLOAT fAlpha;                                                                                                               //指定alpha混合值,必须有一个在0.0到1.0之间的值(含) 。

COLORREF clrSrcKey;                                                                                                  //指定需要取消图片的背景颜色

} VMRALPHABITMAP;typedef struct _VMRALPHABITMAP *PVMRALPHABITMAP;

 

dwFlags应为以下值

 

VMRBITMAP_DISABLE (0x00000001) 现在不需要位图
VMRBITMAP_HDC (0x00000002) 从HDC中获取位图
VMRBITMAP_ENTIREDDS (0x00000004) 从整个DirectDraw surface获取位图.当这个被指定时, 指定rSrc没有用.
VMRBITMAP_SRCCOLORKEY (0x00000008) clrSrcKey 值可用,当在混合运算渲染位图时,这个值将被用到
VMRBITMAP_SRCRECT (0x00000010)

 

HRESULT CVMRPlayMovive::BlendApplicationImage(HWND hwndApp)
{
LONG cx, cy;
HRESULT hr;
RECT rc={0};
hr = pWC->GetNativeVideoSize(&cx, &cy, NULL, NULL);
if (FAILED(hr))
return hr;


HINSTANCE hInstance = (HINSTANCE)GetWindowLong(m_hwnd,GWL_HINSTANCE); 
HBITMAP hbm = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP3));
    //加载位图
BITMAP bm;
HBITMAP hbmOld;
HDC hdc = GetDC(hwndApp);
HDC hdcBmp = CreateCompatibleDC(hdc);
ReleaseDC(hwndApp, hdc);
GetObject(hbm, sizeof(bm), &bm);
hbmOld = (HBITMAP)SelectObject(hdcBmp, hbm);


VMRALPHABITMAP bmpInfo;
ZeroMemory(&bmpInfo, sizeof(bmpInfo) );
bmpInfo.dwFlags = VMRBITMAP_HDC;
bmpInfo.hdc = hdcBmp;




g_nBitmapWidth = bm.bmWidth;
g_nImageWidth  = bm.bmWidth / NUM_IMAGES_IN_BITMAP;
SetRect(&rc,0,0,g_nBitmapWidth, bm.bmHeight);
bmpInfo.rSrc = rc;


// rDest specifies the destination rectangle in composition space (0.0f to 1.0f)
bmpInfo.rDest.left   = (float)(cx - g_nImageWidth) / (float)cx - EDGE_BUFFER;
bmpInfo.rDest.top    = (float)(cy - bm.bmHeight)   / (float)cy - EDGE_BUFFER;
bmpInfo.rDest.right  = 1.0f - EDGE_BUFFER;
bmpInfo.rDest.bottom = 1.0f - EDGE_BUFFER;


g_rDest = bmpInfo.rDest;
g_rSrc  = bmpInfo.rSrc;


g_fBitmapCompWidth = (float)g_nImageWidth / (float)cx;
bmpInfo.fAlpha = 0.0;


SetColorRef(bmpInfo);


hr = pBMP->SetAlphaBitmap(&bmpInfo);
if (FAILED(hr))
Msg(TEXT("SetAlphaBitmap FAILED!  Bitmap operations will fail. hr=0x%x\r\n"), hr);


// Clean up GDI resources
DeleteObject(SelectObject(hdcBmp, hbmOld));
DeleteObject(hbm);
DeleteDC(hdcBmp);


// If setting the alpha bitmap succeeded, update its parameters
if (SUCCEEDED(hr))
{
SetRect(&rc, 0, 0, g_nImageWidth, bm.bmHeight);
bmpInfo.rSrc = rc;
g_rSrcSingle = rc;
bmpInfo.dwFlags = VMRBITMAP_SRCRECT | VMRBITMAP_SRCCOLORKEY;
bmpInfo.fAlpha = TRANSPARENCY_VALUE;
hr = pBMP->UpdateAlphaBitmapParameters(&bmpInfo);
if (FAILED(hr))
Msg(TEXT("UpdateAlphaBitmapParameters FAILED!  hr=0x%x\r\n"), hr);
}


return hr;
}


void CVMRPlayMovive::SetColorRef(VMRALPHABITMAP& bmpInfo)
{
bmpInfo.clrSrcKey = RGB(255, 0, 0);       
bmpInfo.dwFlags |= VMRBITMAP_SRCCOLORKEY;
}

你可能感兴趣的:(C++,视频,mfc,Visual,directshow)