private async Task LoadImage()
 {
     if (_uriSurface == null)
     {
         _firstTimeLoad = true;
         _uriSurface    = await _surfaceFactoryInstance.CreateUriSurfaceAsync(ImageUri);
     }
     else
     {
         _firstTimeLoad = false;
         await _uriSurface.RedrawSurfaceAsync(ImageUri);
     }
 }
        public async Task LoadImageAsync(Uri imageUri)
        {
            if (_uriSurface == null)
            {
                _uriSurface = await _surfaceFactoryInstance.CreateUriSurfaceAsync(imageUri);
            }
            else
            {
                await _uriSurface.RedrawSurfaceAsync(imageUri);
            }
            var brush = _compositor.CreateSurfaceBrush(_uriSurface.Surface);

            brush.Stretch = CompositionStretch.Uniform;
            Visual.Brush  = brush;
            Visual.Size   = new Vector2((float)ActualWidth, (float)ActualHeight);
        }
示例#3
0
        /// <summary>
        /// Load the image and transform it to a composition brush or a XAML brush (depends of the UIStrategy)
        /// </summary>
        /// <param name="uri">the uri of the image to load</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        private async Task <bool> LoadImageBrush(Uri uri)
        {
            var strategy = Strategy;

            if (strategy == UIStrategy.Composition)
            {
                if (_containerVisual == null || uri == null)
                {
                    return(false);
                }
            }
            else
            {
                if (uri == null)
                {
                    return(false);
                }
            }

            await _flag.WaitAsync();

            try
            {
                bool isAnimated = IsAnimated;

                IsAnimated = false;

                if (_isImageSourceLoaded == true)
                {
                    for (int i = 0; i < _compositionChildren.Count; i++)
                    {
                        if (strategy == UIStrategy.PureXaml)
                        {
                            _xamlChildren[i].Fill = null;
                        }
                        else
                        {
                            _compositionChildren[i].Brush = null;
                        }
                    }

                    if (strategy == UIStrategy.Composition)
                    {
                        _brushVisual.Dispose();
                        _brushVisual = null;

                        _uriSurface.Dispose();
                        _uriSurface = null;
                    }
                }

                _isImageSourceLoaded = false;

                if (strategy == UIStrategy.Composition)
                {
                    var compositor     = _containerVisual.Compositor;
                    var surfaceFactory = SurfaceFactory.GetSharedSurfaceFactoryForCompositor(compositor);

                    var surfaceUri = await surfaceFactory.CreateUriSurfaceAsync(uri);

                    _uriSurface  = surfaceUri;
                    _brushVisual = compositor.CreateSurfaceBrush(surfaceUri.Surface);

                    _imageSize = surfaceUri.Size;
                }
                else
                {
                    BitmapImage image = new BitmapImage();

                    var storageFile = await StorageFile.GetFileFromApplicationUriAsync(uri);

                    using (var stream = await storageFile.OpenReadAsync())
                    {
                        image.SetSource(stream);
                    }

                    _brushXaml = new ImageBrush()
                    {
                        ImageSource = image
                    };
                    _imageSize = new Size(image.PixelWidth, image.PixelHeight);
                }

                _isImageSourceLoaded = true;

                RefreshContainerTile();

                RefreshImageSize(_imageSize.Width, _imageSize.Height);

                if (isAnimated == true)
                {
                    IsAnimated = true;
                }
            }
            finally
            {
                _flag.Release();
            }

            ImageLoaded?.Invoke(this, EventArgs.Empty);

            return(true);
        }