示例#1
0
        private async void OnSourceChanged(IVideoAnimationSource newValue, IVideoAnimationSource oldValue)
        {
            var canvas = _canvas;
            if (canvas == null)
            {
                return;
            }

            if (newValue == null)
            {
                _source = null;
                Subscribe(false);
                return;
            }

            if (newValue?.Id == oldValue?.Id || newValue?.Id == _source?.Id)
            {
                return;
            }

            _source = newValue;

            var shouldPlay = _shouldPlay;

            var animation = await Task.Run(() => VideoAnimation.LoadFromFile(newValue, false, true));
            if (animation == null || newValue?.Id != _source?.Id)
            {
                // The app can't access the file specified
                return;
            }

            if (_shouldPlay)
            {
                shouldPlay = true;
            }

            _animation = animation;
            _bitmap = null;

            if (AutoPlay || _shouldPlay)
            {
                _shouldPlay = false;
                Subscribe(true);
            }
            else if (!_unloaded)
            {
                Subscribe(false);

                if (_canvas.ReadyToDraw)
                {
                    var buffer = ArrayPool<byte>.Shared.Rent(_animation.PixelWidth * _animation.PixelHeight * 4);
                    _bitmap = CanvasBitmap.CreateFromBytes(_canvas, buffer, _animation.PixelWidth, _animation.PixelHeight, DirectXPixelFormat.R8G8B8A8UIntNormalized);
                    ArrayPool<byte>.Shared.Return(buffer);
                }

                // Invalidate to render the first frame
                await Task.Run(Invalidate);
                _canvas?.Invalidate();
            }
        }
示例#2
0
        private void OnCreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
        {
            args.TrackAsyncAction(Task.Run(() =>
            {
                var animation = VideoAnimation.LoadFromFile(_source, false, true);
                if (animation == null)
                {
                    return;
                }

                _animation = animation;

                lock (_reusableLock)
                {
                    if (_reusableBuffer == null || _reusableBuffer.Length < _animation.PixelWidth * _animation.PixelHeight * 4)
                    {
                        _reusableBuffer = new byte[_animation.PixelWidth * _animation.PixelHeight * 4];
                    }
                }

                _bitmap = CanvasBitmap.CreateFromBytes(sender, _reusableBuffer, _animation.PixelWidth, _animation.PixelHeight, DirectXPixelFormat.R8G8B8A8UIntNormalized);
                _device = sender;

                // Invalidate to render the first frame
                if (!_subscribed)
                {
                    Invalidate();
                    _canvas?.Invalidate();
                }
            }).AsAsyncAction());
        }
示例#3
0
        private void OnCreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
        {
            args.TrackAsyncAction(Task.Run(() =>
            {
                var animation = VideoAnimation.LoadFromFile(_source, false, true);
                if (animation == null)
                {
                    return;
                }

                _animation = animation;

                var colors = new byte[_animation.PixelWidth * _animation.PixelHeight * 4];

                _bitmap = CanvasBitmap.CreateFromBytes(sender, colors, _animation.PixelWidth, _animation.PixelHeight, Windows.Graphics.DirectX.DirectXPixelFormat.R8G8B8A8UIntNormalized);
                _device = sender;
            }).AsAsyncAction());
        }