DirectShow 录制视频时,如何在视频上画线和输出文字

下面的代码说明了如何在录制视频的时候在视频中加入文字或其他的形状


LONG cx, cy;
HRESULT hr;
hr = pWC->GetNativeVideoSize(&cx, &cy, NULL, NULL);
if (FAILED(hr))
{
Msg(TEXT("GetNativeVideoSize FAILED!  hr=0x%x\r\n"), hr);
return hr;
}


HDC hdc = GetDC(hwndApp);
HDC hdcBmp = CreateCompatibleDC(hdc);
g_hFont=CreateFont(30, 10,0,0, 10, FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,_T("宋体"));
HFONT hOldFont = (HFONT) SelectObject(hdcBmp, g_hFont);


HPEN hPen=CreatePen(PS_SOLID,1,RGB(255,0,0));
HPEN hOldPen = (HPEN) SelectObject(hdcBmp, hPen);


int nLength, nTextBmpWidth, nTextBmpHeight;
SIZE sz={0};
nLength = (int) _tcslen(szNewText);
GetTextExtentPoint32(hdcBmp, szNewText, nLength, &sz);
nTextBmpHeight = sz.cy;
nTextBmpWidth  = 400/*sz.cx*/;


HBITMAP hbm = CreateCompatibleBitmap(hdc, nTextBmpWidth, nTextBmpHeight);
ReleaseDC(hwndApp, hdc);


BITMAP bm;
HBITMAP hbmOld;
GetObject(hbm, sizeof(bm), &bm);
hbmOld = (HBITMAP)SelectObject(hdcBmp, hbm);


RECT rcText;
SetRect(&rcText, 0, 0, nTextBmpWidth, nTextBmpHeight);
SetBkMode(hdcBmp,TRANSPARENT);            //设置透明背景
SetTextColor(hdcBmp, g_rgbColors);      // 文字颜色
HBRUSH hBrush=((HBRUSH)GetStockObject(NULL_BRUSH));
    SelectObject(hdcBmp, hBrush);
// 在位图上画图
//输出文字
//TextOut(hdcBmp, 0, 0, szNewText, nLength);
DrawText(hdcBmp,szNewText,sizeof(szNewText),&rcText,DT_RIGHT);
//画线
MoveToEx(hdcBmp,10,10,NULL);
LineTo(hdcBmp,200,10);
//Ellipse(hdcBmp,0,0,100,nTextBmpHeight);


// Configure the VMR's bitmap structure
VMRALPHABITMAP bmpInfo;
ZeroMemory(&bmpInfo, sizeof(bmpInfo) );
bmpInfo.dwFlags = VMRBITMAP_HDC;
bmpInfo.hdc = hdcBmp;  
g_nImageWidth = bm.bmWidth;
g_fBitmapCompWidth = (float)g_nImageWidth / (float)cx;


bmpInfo.rDest.left  = 0.0f + X_EDGE_BUFFER;
bmpInfo.rDest.right = 1.0f - X_EDGE_BUFFER;
bmpInfo.rDest.top = (float)(cy - bm.bmHeight) / (float)cy - Y_EDGE_BUFFER;
bmpInfo.rDest.bottom = 1.0f - Y_EDGE_BUFFER;
bmpInfo.rSrc = rcText;




bmpInfo.fAlpha = TRANSPARENCY_VALUE;
SetColorRef(bmpInfo);


hr = pBMP->SetAlphaBitmap(&bmpInfo);
if (FAILED(hr))
Msg(TEXT("SetAlphaBitmap FAILED!  hr=0x%x\r\n\r\n%s\0"), hr,
STR_VMR_DISPLAY_WARNING);


// Select the initial objects back into our device context
DeleteObject(SelectObject(hdcBmp, hbmOld));
DeleteObject(SelectObject(hdcBmp, hOldPen));
SelectObject(hdc, hOldFont);
DeleteObject(hbm);
DeleteDC(hdcBmp);
return hr;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值