You can overlay frames with other frames to make a digital signature, channel logo or picture-in-picture. To make overlay you need at least 2 MFFrames object: one as a base frame and another as an overlay. You also can implement mask for overlay frame with MFormats SDK. The overlay is implemented to base MFFrame object with MFOverlay or MFOverlayRect methods.
When both frames (base and overlay) contains audio the resulting audio is mixed. By default, the audio gain is calculated based on overlay size and transparency of overlay frame. You can specify audio gain with "audio_gain" additional property (e.g. "audio_gain=-10"). The value is in decibels.
Source frame and frame for overlay in this article are:
// grab frame from source MFFrame sourceFrame; mySource.SourceFrameGet(-1, out sourceFrame, ""); // create a frame from file MFFactory frameFactory = new MFFactory(); string pathToLocalFile = @"c:\channelLogo.png"; MFFrame frameToOverlay; frameFactory.MFFrameCreateFromFile(pathToLocalFile, out frameToOverlay, "");
You can get mask frame from another source or create it from a file the similar way.
Overlay with MFOverlay method
MFOverlay method contains these parameters:
- _pFrameOverlay - frame that is used for overlay
- _pFrameMask - frame for masking
- _nPosX - horizontal position of overlay in pixels. The position is calculated from the top-left corner.
- _nPosY - vertical position of overlay in pixels. The position is calculated from the top-left corner.
- _dblAlpha - transparency (0 means transparent, 1.0 means solid)
- _bsPropsList - additional properties. You can specify audio gain ("audio_gain") in this parameter.
- _bsAudioBufferID - ID of audio buffer.This property is reserved for future usage.
For example, to overlay source frame with logo and place the image at (30, 20) point and make it slightly transparent:
// set parameters for overlay int overlayX = 30; int overlayY = 20; double overlayTransparency = 0.9; // make overlay sourceFrame.MFOverlay(frameToOverlay, null, overlayX, overlayY, overlayTransparency, "", "");
Mask frame is not implemented in this example so null is used instead.
MFOverlay method uses the full size of frameToOverlay, so if it is large enough it may overlay too large part of the base frame. To avoid this effect you can resize a new frame with the MFResize method.
Overlay with MFOverlayRect method
You can place overlay to the required rectangle with the MFOverlayRect method. This method contains the following parameters:
- _pFrameOverlay - frame for overlay
- _pFrameMask - frame mask
- _pRect - rectangle for overlay
- _eMFOverlayFlags - overlay flags to specify scaling for overlay frame
- _dblAlpha - transparency (0 means transparent, 1.0 means solid)
- _bsPropsList - additional properties. You can specify audio gain ("audio_gain") in this parameter.
- _bsAudioBufferID - ID of audio buffer.This property is reserved for future usage.
For example, to overlay source frame with logo and place the image into the rectangle with width = 160 pixels, height = 120 pixels in point (30, 20):
MF_RECT overlayRect = new MF_RECT(); overlayRect.dblHeight = 120; overlayRect.dblWidth = 160; overlayRect.dblPosX = 30; overlayRect.dblPosY = 20; overlayRect.eRectType = eMFRectType.eMFRT_Absolute; double overlayTransparency = 0.9; eMFOverlayFlags overlayFlags = eMFOverlayFlags.eMFOF_Crop; pFrame.MFOverlayRect(frameToOverlay, null, ref overlayRect, overlayFlags, overlayTransparency , "", "");
Mask frame is not implemented in this example so null is used instead.
With this method, you can use frames of any size to overlay. The overlay frame is scaled to fill target rectangle according to overlay flags (eMFOverlayFlags enumeration).
Mask processing
Every image you use as a mask is first converted to the grayscale RGB image. Alpha component of your image is also converted to grayscale: transparent parts (alpha=0) converted to black color and non-transparent parts (alpha=255) to white.
Next, this RGB grayscale mask is used to calculate the transparency of the overlay in the opposite way: black color provides transparent parts (alpha=0) and white color provides non-transparent parts (alpha=255).
We advice using regular RGB grayscale images as a mask, to get the easily predictable result.