/// <summary>
 /// Redraws the IImageMaskSurface using the alpha values of the image in the given IImageMaskSurface using the given padding
 /// and options.
 /// </summary>
 /// <param name="imageMaskSurface">IImageMaskSurface whose image's alpha values are to be used to create the mask.</param>
 /// <param name="padding">The padding between the IImageMaskSurface outer bounds and the bounds of the area where
 /// the mask, created from the loaded image's alpha values, should be rendered.</param>
 /// <param name="options">Describes the image's resize, alignment and blur radius options in the allocated space.</param>
 public void Redraw(IImageMaskSurface imageMaskSurface, Thickness padding, ImageSurfaceOptions options)
 {
     if (imageMaskSurface != null)
     {
         Redraw(imageMaskSurface.SurfaceBitmap, imageMaskSurface.Size, padding, options);
     }
     else
     {
         // Draw an empty surface
         Redraw(surfaceBitmap: null, Size, padding, options);
     }
 }
 /// <summary>
 /// Redraws the IImageMaskSurface using the alpha values of the image in the given IImageMaskSurface.
 /// </summary>
 /// <param name="imageMaskSurface">IImageMaskSurface whose image's alpha values are to be used to create the mask.</param>
 public void Redraw(IImageMaskSurface imageMaskSurface)
 {
     if (imageMaskSurface != null)
     {
         Redraw(imageMaskSurface.SurfaceBitmap, imageMaskSurface.Size, imageMaskSurface.MaskPadding, imageMaskSurface.Options);
     }
     else
     {
         // Draw an empty surface
         Redraw(surfaceBitmap: null, Size, MaskPadding, Options);
     }
 }
예제 #3
0
        async void Redraw(bool recreateSurface = true)
        {
            if (_compositor == null)
            {
                _compositor = Window.Current.Compositor;
                _generator  = _compositor.CreateCompositionGenerator();

                _defaultSize = new Vector2(GridWidth, GridWidth);
                // Create the original output image visual
                _originalImageVisual      = _compositor.CreateSpriteVisual();
                _originalImageVisual.Size = _defaultSize;
                _imageSurface             = await _generator.CreateImageSurfaceAsync(new Uri("ms-appx:///Assets/Images/cat.png"), _defaultSize.ToSize(), ImageSurfaceOptions.Default);

                _originalImageVisual.Brush = _compositor.CreateSurfaceBrush(_imageSurface);
                ElementCompositionPreview.SetElementChildVisual(OriginalOutputGrid, _originalImageVisual);

                // Create the source image visual
                _sourceImageVisual      = _compositor.CreateSpriteVisual();
                _sourceImageVisual.Size = _defaultSize;
                ElementCompositionPreview.SetElementChildVisual(ImageGrid, _sourceImageVisual);

                //Create the mask visual
                _maskVisual      = _compositor.CreateSpriteVisual();
                _maskVisual.Size = _defaultSize;
                ElementCompositionPreview.SetElementChildVisual(MaskGrid, _maskVisual);

                // Create the output visual
                _outputVisual      = _compositor.CreateSpriteVisual();
                _outputVisual.Size = _defaultSize;
                ElementCompositionPreview.SetElementChildVisual(OutputGrid, _outputVisual);
            }

            var selIndex = ImageList.SelectedIndex;

            if (selIndex == -1)
            {
                return;
            }

            var uri = _images.Values.ElementAt(selIndex);

            if (uri == null)
            {
                return;
            }

            var offset = OffsetSlider.Value.ToSingle();

            var padding = new Thickness(offset);

            if (recreateSurface)
            {
                _sourceImageSurface =
                    await _generator.CreateImageSurfaceAsync(uri, _defaultSize.ToSize(), ImageSurfaceOptions.Default);

                _imageMaskSurface = _generator.CreateImageMaskSurface(_sourceImageSurface, _defaultSize.ToSize(),
                                                                      padding, ImageSurfaceOptions.GetDefaultImageMaskOptionsForBlur(_blurRadius));
            }
            else
            {
                _imageMaskSurface.Resize(_defaultSize.ToSize(), padding, ImageSurfaceOptions.GetDefaultImageMaskOptionsForBlur(_blurRadius));
            }

            _sourceImageVisual.Brush = _compositor.CreateSurfaceBrush(_sourceImageSurface);
            _maskVisual.Brush        = _compositor.CreateSurfaceBrush(_imageMaskSurface);
            var maskedBrush = _compositor.CreateMaskBrush();

            maskedBrush.Source = _originalImageVisual.Brush;
            maskedBrush.Mask   = _compositor.CreateSurfaceBrush(_imageMaskSurface);

            _outputVisual.Brush = maskedBrush;
        }
 /// <summary>
 /// Resizes and redraws the IImageMaskSurface using the alpha values of the image in the given IImageMaskSurface
 /// with the given padding and options.
 /// </summary>
 /// <param name="imageMaskSurface">IImageMaskSurface whose image's alpha values are to be used to create the mask.</param>
 /// <param name="size">New size of the IImageMaskSurface.</param>
 /// <param name="padding">The padding between the IImageMaskSurface outer bounds and the bounds of the area where
 /// the mask, created from the loaded image's alpha values, should be rendered.</param>
 /// <param name="options">Describes the image's resize, alignment and blur radius options in the allocated space.</param>
 public void Redraw(IImageMaskSurface imageMaskSurface, Size size, Thickness padding, ImageSurfaceOptions options)
 {
     Redraw(imageMaskSurface?.SurfaceBitmap, size, padding, options);
 }