You can add text overlays to a video frame by using a single method - MFPrint.
This method includes the following parameters:
- _bsText – the text you would like to overlay.
- _dblTextSize – font size measured in relative units, where size 1.0 is 1/24 of screen height.
- _pRect – the rectangle area (MF_RECT) where the text overlay will appear.
- _eMFTextFlags – a set of additional flags (eMFTextFlags) to align the text.
- _bsExtraParam – a string with additional parameters for text formatting (described below)
Parameter _bsText can be just plain text or it can contain the following XML tags:
- text (font) – style defined by parameters: face (e.g., "Times New Roman", "Arial", "Tahoma", etc.), size (in ems) and color.
- img – embedded image with required parameter src = <path to image> and optional parameters width, height and size = <height relative to text line> (e.g., if size = 2.0 image height will be equal to double text line height).
- b – bold.
- i – italics.
- u – underline.
- s – strike out.
Additional parameters are introduced as a string in order to achieve greater flexibility: adding new formatting options will not change the code for calling the method.
In order to add formatting, we can change the _bsExtraParam. Here are the available parameters:
- face – font name, such as 'Times New Roman', 'Arial', etc.
- weight – font weight with possible numeric values from 0.0 to 2.0.
- italic – italic type. Possible values are true and false.
- bold – boldface type. Possible values are true and false.
- oblique – bold italic type. Possible values are true and false.
- stretch – font-stretch with possible numeric values from 0.0 to 2.0.
- word-wrap – automatically moving the word to the next line if a line of text is filled. Possible values are true and false.
- line-spacing – line height, or distance between one baseline to another. Possible values are from 0.0 to 10.0. By default, the line height is 1.0
- baseline – the distance from the top of the line to baseline. By default, the baseline is 0.8.
- align – horizontal alignment of text. Possible values are: left, center, right and justify.
- vert-align – vertical alignment of text. Possible values are top, center, and bottom.
- color – font color. HEX-code value ('0x00f0ff') or color list ('red', 'green', 'blue', etc.). Can be defined with a transparency level in brackets: 'red(50)', '0xffffff(80)'.
- outline – width of text outline measured in relative units, where outline=1.0 is an outline with the width of 1/240 of screen height.
- outline-color – color of text outline. HEX code value, for example, '0xff0000' or color list (red, green, blue, etc.). Can be defined with a transparency level in brackets: 'red(50)', '0xffffff(80)'.
- bg-color – background color. HEX code value, for example, '0xff0000' or color list (red, green, blue, etc.). Can be defined with a transparency level in brackets: 'red(50)', '0xffffff(80)'.
A sample text overlay looks like:
// grab frame from source MFFrame sourceFrame; mySource.SourceFrameGet(-1, out sourceFrame, ""); // configure parameters for overlay string textToOverlay = "My Sample Text"; double textSize = 3.0; MF_RECT textRectangle= new MF_RECT(); textRectangle.dblHeight = 300; textRectangle.dblPosX = 100; textRectangle.dblPosY = 150; textRectangle.dblWidth = 600; textRectangle.eRectType = eMFRectType.eMFRT_Absolute; eMFTextFlags textFlags = eMFTextFlags.eMFT_WordBreaks; string formattingParameters = "face='Times New Roman' weight='1.5' italic='true' bold='true' align='justify' vert-align='center' color='red'"; // overlay text sourceFrame.MFPrint(textToOverlay, textSize, ref textRectangle, textFlags, formattingParameters); // put frame to receiver myReceiver.ReceiverFramePut(sourceFrame, -1, "");
Here is example of this code from Draw Frames Sample:
<text color=red>M</text>Formats <text size=0.9 face='Courier New'><b>IMFFrame::MFPrint</b></text> support:
<tab>- Different <text color=yellow>colors</text>
<tab>- Different <text size=1.0 face='Courier New'>fonts</text>
<tab>- Different styles: <b>bold</b> <i>italic</i> <u>underline</u> <s>strikeout</s>
<tab>- Different <text size=2.0>text</text> <text size=0.5>size</text>
<tab>- Add even pictures: <img size=3.0 src="C:\Users\Vsevolod\Pictures\logo.png"</img>

When using more than 16 threads for MFPrint, each creating its own CD2DTextRenderer instance, you may encounter text flickering. This occurs because the internal renderer cache is limited by default to 16 threads. Once this limit is exceeded, older renderers may be removed and recreated periodically (every 3 seconds by default), which can lead to rendering instability or exceptions in Direct3D.
To avoid this, you can adjust the renderer cache settings via the following registry keys under
Computer\HKEY_CURRENT_USER\Software\Medialooks\MFormats\MFFactory:
-
text_renderers.cache.min — minimum number of cached renderers (default 3)
-
text_renderers.cache.max — maximum number of cached renderers (default 16)
-
text_renderers.cache.time — lifetime of unused renderers in seconds (default 3)
In addition, you can configure these values programmatically to match the number of concurrent threads in use: