Ejemplo n.º 1
0
        private ICanvasImage CreateChromaKey()
        {
            textLabel = requiresWin10;

            var chromaKeyEffect = new ChromaKeyEffect
            {
                Source  = bitmapTiger,
                Color   = Color.FromArgb(255, 162, 125, 73),
                Feather = true
            };

            // Composite the chromakeyed image on top of a background checker pattern.
            Color[] twoByTwoChecker =
            {
                Colors.LightGray, Colors.DarkGray,
                Colors.DarkGray,  Colors.LightGray,
            };

            var compositeEffect = new CompositeEffect
            {
                Sources =
                {
                    // Create the checkered background by scaling up a tiled 2x2 bitmap.
                    new CropEffect
                    {
                        Source = new DpiCompensationEffect
                        {
                            Source = new ScaleEffect
                            {
                                Source = new BorderEffect
                                {
                                    Source  = CanvasBitmap.CreateFromColors(canvas, twoByTwoChecker, 2, 2),
                                    ExtendX = CanvasEdgeBehavior.Wrap,
                                    ExtendY = CanvasEdgeBehavior.Wrap
                                },
                                Scale             = new Vector2(8, 8),
                                InterpolationMode = CanvasImageInterpolation.NearestNeighbor
                            }
                        },
                        SourceRectangle = bitmapTiger.Bounds
                    },

                    // Composite the chromakey result on top of the background.
                    chromaKeyEffect
                }
            };

            // Animation changes the chromakey matching tolerance.
            animationFunction = elapsedTime =>
            {
                chromaKeyEffect.Tolerance = 0.1f + (float)Math.Sin(elapsedTime) * 0.1f;
            };

            return(compositeEffect);
        }
Ejemplo n.º 2
0
        public async void ProcessFrame(ProcessVideoFrameContext context)
        {
            // When using SupportedMemoryTypes => MediaMemoryTypes.GpuAndCpu we need to check if we're using GPU or CPU for the frame

            // If we're on GPU, use InputFrame.Direct3DSurface
            if (context.InputFrame.SoftwareBitmap == null)
            {
                using (var inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, context.InputFrame.Direct3DSurface))
                    using (var renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface))
                        using (var ds = renderTarget.CreateDrawingSession())
                        {
                            var chromaKeyEffect = new ChromaKeyEffect
                            {
                                Color     = ChromaColor,
                                Source    = inputBitmap,
                                Tolerance = Tolerance,
                                Feather   = true
                            };

                            //only load bg image if it hasnt already been loaded or if image is different
                            if (_backgroundBitmap == null || _lastUsedImageFilePath != _imageFileName)
                            {
                                _backgroundBitmap = await CanvasBitmap.LoadAsync(ds, new Uri($"ms-appdata:///Local/{ImageFileName}"));
                            }

                            _lastUsedImageFilePath = _imageFileName; //keep track of any incoming image changes

                            var compositeEffect = new CompositeEffect
                            {
                                Sources = { _backgroundBitmap, chromaKeyEffect }
                            };

                            ds.DrawImage(compositeEffect);
                        }

                return;
            }

            // If we're on CPU, use InputFrame.SoftwareBitmap
            if (context.InputFrame.Direct3DSurface == null)
            {
                // InputFrame's raw pixels
                byte[] inputFrameBytes = new byte[4 * context.InputFrame.SoftwareBitmap.PixelWidth * context.InputFrame.SoftwareBitmap.PixelHeight];
                context.InputFrame.SoftwareBitmap.CopyToBuffer(inputFrameBytes.AsBuffer());

                using (var inputBitmap = CanvasBitmap.CreateFromBytes(
                           _canvasDevice,
                           inputFrameBytes,
                           context.InputFrame.SoftwareBitmap.PixelWidth,
                           context.InputFrame.SoftwareBitmap.PixelHeight,
                           context.InputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat()))

                    using (var renderTarget = new CanvasRenderTarget(
                               _canvasDevice,
                               context.OutputFrame.SoftwareBitmap.PixelWidth,
                               context.OutputFrame.SoftwareBitmap.PixelHeight,
                               (float)context.OutputFrame.SoftwareBitmap.DpiX,
                               context.OutputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat(),
                               CanvasAlphaMode.Ignore))
                    {
                        using (var ds = renderTarget.CreateDrawingSession())
                        {
                            var chroma = new ChromaKeyEffect
                            {
                                Color     = ChromaColor,
                                Source    = inputBitmap,
                                Tolerance = Tolerance
                            };

                            //only load bg image if it hasnt already been loaded or if image is different
                            if (_backgroundBitmap == null || _lastUsedImageFilePath != _imageFileName)
                            {
                                _backgroundBitmap = await CanvasBitmap.LoadAsync(ds, new Uri($"ms-appdata:///Local/{ImageFileName}"));
                            }

                            //keep track of any incoming image changes
                            _lastUsedImageFilePath = _imageFileName;

                            var compositeEffect = new CompositeEffect {
                                Mode = CanvasComposite.Add
                            };
                            compositeEffect.Sources.Add(_backgroundBitmap);
                            compositeEffect.Sources.Add(chroma);

                            ds.DrawImage(compositeEffect);
                        }
                    }
            }
        }
Ejemplo n.º 3
0
 public ChromaKeyFrame(ChromaKeyEffect targetEffect) : base(targetEffect)
 {
 }