From f0bbcae864457fab6d6d9d5a1d394e691b12cdcb Mon Sep 17 00:00:00 2001 From: United600 Date: Sun, 19 Oct 2025 19:08:20 +0100 Subject: [PATCH] Update IVideoCompositor example and remarks code blocks --- windows.media.effects/ivideocompositor.md | 268 +++++++++++----------- 1 file changed, 136 insertions(+), 132 deletions(-) diff --git a/windows.media.effects/ivideocompositor.md b/windows.media.effects/ivideocompositor.md index 55b6ed935b..6e9955d760 100644 --- a/windows.media.effects/ivideocompositor.md +++ b/windows.media.effects/ivideocompositor.md @@ -16,6 +16,7 @@ The interface you implement to create a custom video compositor. 1. In a Windows Runtime Component project, derive a public sealed class from this interface (see example below). 2. Use the full class name in a [MediaOverlayLayer](/uwp/api/windows.media.editing.mediaoverlaylayer) constructor. + ```csharp var propertySet = new PropertySet { ["Feather"] = true, @@ -28,166 +29,169 @@ The interface you implement to create a custom video compositor. propertySet); var mediaOverlayLayer = new MediaOverlayLayer(compositorDefinition); + ``` ## -examples - - using Microsoft.Graphics.Canvas; - using Microsoft.Graphics.Canvas.Effects; - using Windows.Foundation.Collections; - using Windows.Graphics.DirectX.Direct3D11; - using Windows.Media.Effects; - using Windows.Media.MediaProperties; - using Windows.UI; - - namespace Effects + +```csharp +using Microsoft.Graphics.Canvas; +using Microsoft.Graphics.Canvas.Effects; +using Windows.Foundation.Collections; +using Windows.Graphics.DirectX.Direct3D11; +using Windows.Media.Effects; +using Windows.Media.MediaProperties; +using Windows.UI; + +namespace Effects +{ + /// + /// Chroma key compositor + /// + /// Properties + /// Color (Color) : Chroma key color (default is black) + /// Feather (Boolean): true to soften edges of the output (default is false) + /// Tolerance (float): Color tolerance 0-1 (default is 0.1) + /// InvertAlpha (Boolean): invert the alpha value (default is false) + /// + /// + public sealed class ChromaKeyVideoCompositor : IVideoCompositor { + #region Fields + private VideoEncodingProperties _backgroundProperties; + private CanvasDevice _canvasDevice; + #endregion + + #region Properties + /// + /// Gets the chroma-key color + /// + public Color Color { get; private set; } = Colors.Black; + + /// + /// Gets a value indicating whether to feather the edges of the chroma key + /// + public bool Feather { get; private set; } = false; + + /// + /// Gets the color tolerance + /// + public float Tolerance { get; private set; } = 0.1f; + /// - /// Chroma key compositor - /// - /// Properties - /// Color (Color) : Chroma key color (default is black) - /// Feather (Boolean): true to soften edges of the output (default is false) - /// Tolerance (float): Color tolerance 0-1 (default is 0.1) - /// InvertAlpha (Boolean): invert the alpha value (default is false) - /// + /// Gets a value indicating whether to invert the alpha transparency /// - public sealed class ChromaKeyVideoCompositor : IVideoCompositor + public bool InvertAlpha { get; private set; } = false; + + /// + /// Gets a value indicating whether the compositor is time-independent + /// + public bool TimeIndependent => true; + #endregion + + #region Methods + /// + /// Sets the encoding properties + /// + /// the background properties + /// the Direct3D device + public void SetEncodingProperties(VideoEncodingProperties backgroundProperties, IDirect3DDevice device) { - #region Fields - private VideoEncodingProperties _backgroundProperties; - private CanvasDevice _canvasDevice; - #endregion - - #region Properties - /// - /// Gets the chroma-key color - /// - public Color Color { get; private set; } = Colors.Black; - - /// - /// Gets a value indicating whether to feather the edges of the chroma key - /// - public bool Feather { get; private set; } = false; - - /// - /// Gets the color tolerance - /// - public float Tolerance { get; private set; } = 0.1f; - - /// - /// Gets a value indicating whether to invert the alpha transparency - /// - public bool InvertAlpha { get; private set; } = false; - - /// - /// Gets a value indicating whether the compositor is time-independent - /// - public bool TimeIndependent => true; - #endregion - - #region Methods - /// - /// Sets the encoding properties - /// - /// the background properties - /// the Direct3D device - public void SetEncodingProperties(VideoEncodingProperties backgroundProperties, IDirect3DDevice device) - { - _backgroundProperties = backgroundProperties; + _backgroundProperties = backgroundProperties; - _canvasDevice = CanvasDevice.CreateFromDirect3D11Device(device); - } + _canvasDevice = CanvasDevice.CreateFromDirect3D11Device(device); + } - /// - /// Composite the frame - /// - /// the composite frame context - public void CompositeFrame(CompositeVideoFrameContext context) + /// + /// Composite the frame + /// + /// the composite frame context + public void CompositeFrame(CompositeVideoFrameContext context) + { + foreach (var surface in context.SurfacesToOverlay) { - foreach (var surface in context.SurfacesToOverlay) + using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, surface)) + using (CanvasRenderTarget renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface)) + using (CanvasDrawingSession ds = renderTarget.CreateDrawingSession()) + using (var chromaKeyEffect = new ChromaKeyEffect { - using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, surface)) - using (CanvasRenderTarget renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface)) - using (CanvasDrawingSession ds = renderTarget.CreateDrawingSession()) - using (var chromaKeyEffect = new ChromaKeyEffect - { - Source = inputBitmap, - Color = Color, - Feather = Feather, - Tolerance = Tolerance, - InvertAlpha = InvertAlpha - }) - { - var overlay = context.GetOverlayForSurface(surface); - - var destinationRectangle = overlay.Position; - - var sourceRectangle = inputBitmap.Bounds; + Source = inputBitmap, + Color = Color, + Feather = Feather, + Tolerance = Tolerance, + InvertAlpha = InvertAlpha + }) + { + var overlay = context.GetOverlayForSurface(surface); + + var destinationRectangle = overlay.Position; + + var sourceRectangle = inputBitmap.Bounds; - var opacity = System.Convert.ToSingle(overlay.Opacity); + var opacity = System.Convert.ToSingle(overlay.Opacity); - ds.DrawImage(chromaKeyEffect, destinationRectangle, sourceRectangle, opacity); - } + ds.DrawImage(chromaKeyEffect, destinationRectangle, sourceRectangle, opacity); } } + } - /// - /// Close the compositor & dispose of the canvas device - /// - /// the media effect closed reason - public void Close(MediaEffectClosedReason reason) + /// + /// Close the compositor & dispose of the canvas device + /// + /// the media effect closed reason + public void Close(MediaEffectClosedReason reason) + { + if (_canvasDevice != null) { - if (_canvasDevice != null) - { - _canvasDevice.Dispose(); - _canvasDevice = null; - } + _canvasDevice.Dispose(); + _canvasDevice = null; } + } - /// - /// Discard of the queued frames - /// - /// this does nothing - public void DiscardQueuedFrames() - { - } + /// + /// Discard of the queued frames + /// + /// this does nothing + public void DiscardQueuedFrames() + { + } - /// - /// Sets the properties passed into the compositor - /// - /// the configuration - public void SetProperties(IPropertySet configuration) + /// + /// Sets the properties passed into the compositor + /// + /// the configuration + public void SetProperties(IPropertySet configuration) + { + if (configuration == null) { - if (configuration == null) - { - return; - } + return; + } - object value; + object value; - if (configuration.TryGetValue("Color", out value)) - { - Color = (Color)value; - } + if (configuration.TryGetValue("Color", out value)) + { + Color = (Color)value; + } - if (configuration.TryGetValue("Tolerance", out value)) - { - Tolerance = (float)value; - } + if (configuration.TryGetValue("Tolerance", out value)) + { + Tolerance = (float)value; + } - if (configuration.TryGetValue("Feather", out value)) - { - Feather = (bool)value; - } + if (configuration.TryGetValue("Feather", out value)) + { + Feather = (bool)value; + } - if (configuration.TryGetValue("InvertAlpha", out value)) - { - InvertAlpha = (bool)value; - } + if (configuration.TryGetValue("InvertAlpha", out value)) + { + InvertAlpha = (bool)value; } - #endregion } + #endregion } +} +``` ## -see-also [IMediaExtension](../windows.media/imediaextension.md)