【问题标题】:UWP Masking an image using another image as a maskUWP 使用另一个图像作为遮罩来遮罩图像
【发布时间】:2019-02-05 17:26:18
【问题描述】:

有没有人知道在 UWP 中使用图像作为另一个图像的蒙版的任何方法,我能看到的唯一蒙版功能是 CompositionMaskBrush,我不相信它可以实现我想要的。 我希望实现的一个示例如下。 我有一个手机壳形状的纯黑色 PNG,用户添加自己的图像,然后将其裁剪并遮盖到纯黑色 PNG 的尺寸 - 产生下图。

任何帮助将不胜感激。我花了很长时间浏览解决方案。

Example Image Here

【问题讨论】:

  • 你检查过这个case吗?
  • 是的。据我所知,这需要打开我不需要的捕获设备,例如相机等。图像已由用户加载到应用程序中。
  • 我的意思是你也可以创建透明的手机图片作为蒙版并覆盖在背景图像上。然后使用RenderTargetBitmap 捕获它。
  • 这是我的问题,如何使用透明图像作为蒙版......据我所知,没有真正的方法,或者在东部没有关于它的文档。我能找到的最接近的是 -github.com/ratishphilip/CompositionProToolkit
  • 我想我现在明白你的意思了......我的问题是,我的蒙版文件是一个透明的.png,带有黑色的手机形状。我需要用户图像只出现在黑色像素上。本质上掩盖了图像。现在,我可以在 XAML 中使用几何形状来做到这一点,但我找不到任何可以让我仅在 C# UWP 中绘制黑色像素的东西。

标签: c# xaml uwp mask uwp-xaml


【解决方案1】:

只是为其他需要并回答这个问题的人发帖,但我终于设法找到了使用 Win2D 和 Imageloader 的解决方案。

这是 ImageLoader 的链接。请注意,我必须回滚几个版本才能使其按文档说明的方式工作。下面的链接指向我正在使用的版本。此版本之后的任何内容都不适用于我将发布的示例代码。 https://www.nuget.org/packages/Robmikh.Util.CompositionImageLoader/0.4.0-alpha

    private Compositor _compositor;
    private IImageLoader _imageLoader;
    private CompositionEffectFactory _effectFactory;

    private async void InitMask()
    {


        // Store our Compositor and create our ImageLoader.
        _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
        _imageLoader = ImageLoaderFactory.CreateImageLoader(_compositor);

        // Setup our effect definition. First is the CompositeEffect that will take
        // our sources and produce the intersection of the images (because we selected
        // the DestinationIn mode for the effect). Next we take our CompositeEffect 
        // and make it the source of our next effect, the InvertEffect. This will take 
        // the intersection image and invert the colors. Finally we take that combined 
        // effect and put it through a HueRotationEffect, were we can adjust the colors
        // using the Angle property (which we will animate below). 
        IGraphicsEffect graphicsEffect = new HueRotationEffect
        {
            Name = "hueEffect",
            Angle = 0.0f,
            Source = new InvertEffect
            {
                Source = new CompositeEffect
                {
                    Mode = CanvasComposite.DestinationIn,
                    Sources =
                    {
                        new CompositionEffectSourceParameter("image"),
                        new CompositionEffectSourceParameter("mask")
                    }
                }
            }
        };
        // Create our effect factory using the effect definition and mark the Angle
        // property as adjustable/animatable.
        _effectFactory = _compositor.CreateEffectFactory(graphicsEffect, new string[] { "hueEffect.Angle" });

        // Create MangedSurfaces for both our base image and the mask we'll be using. 
        // The mask is a transparent image with a white circle in the middle. This is 
        // important since the CompositeEffect will use just the circle for the 
        // intersectionsince the rest is transparent.

        var managedImageSurface = await _imageLoader.CreateManagedSurfaceFromUriAsync(new Uri("http://sendus.pics/uploads/" + ImagePass + "/0.png", UriKind.Absolute));
        //var managedImageSurface = await _imageLoader.CreateManagedSurfaceFromUriAsync(new Uri("ms-appx:///Assets/colour.jpg", UriKind.Absolute));
        var managedMaskSurface = await _imageLoader.CreateManagedSurfaceFromUriAsync(new Uri("ms-appx:///" + MaskImage, UriKind.Absolute));

        // Create brushes from our surfaces.
        var imageBrush = _compositor.CreateSurfaceBrush(managedImageSurface.Surface);
        var maskBrush = _compositor.CreateSurfaceBrush(managedMaskSurface.Surface);

        // Create an setup our effect brush.Assign both the base image and mask image
        // brushes as source parameters in the effect (with the same names we used in
        // the effect definition). If we wanted, we could create many effect brushes
        // and use different images in all of them.
        var effectBrush = _effectFactory.CreateBrush();
        effectBrush.SetSourceParameter("image", imageBrush);
        effectBrush.SetSourceParameter("mask", maskBrush);

        // All that's left is to create a visual, assign the effect brush to the Brush
        // property, and attach it into the tree...
        var visual = _compositor.CreateSpriteVisual();

        visual.Size = new Vector2(MaskH, MaskW);
        visual.Offset = new Vector3(0, 300, 0);

        visual.Brush = effectBrush;

        ElementCompositionPreview.SetElementChildVisual(this, visual);
    }

【讨论】:

    猜你喜欢
    • 2011-04-07
    • 2022-10-23
    • 2018-05-29
    • 2016-01-07
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    相关资源
    最近更新 更多