gpt4 book ai didi

c# - 带图像模糊 UWP 的投影

转载 作者:行者123 更新时间:2023-11-30 16:41:14 24 4
gpt4 key购买 nike

我想获得 Windows 视频应用模糊的结果。我做了一个带有阴影的图像,但只给阴影一个颜色。是否有可能使用 win2d 或其他软件获得此结果,然后将图像用于 DropShadow?

enter image description here

我的 MainPage.xaml:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid Width="200" Height="200" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid x:Name="grid"/>
<Image x:Name="image" Source="Assets/image.jpg"/>
</Grid>
</Grid>

我的 MainPage.xaml.cs:

private void Page_Loaded(object sender, RoutedEventArgs e)
{
var compositor = ElementCompositionPreview.GetElementVisual(grid).Compositor;
var spriteVisual = compositor.CreateSpriteVisual();
spriteVisual.Size = new Vector2(200, 200);
var dropShadow = compositor.CreateDropShadow();
dropShadow.Offset = new Vector3(10, 10, 0);
dropShadow.BlurRadius = 10;
dropShadow.Color = Colors.Orange;
spriteVisual.Shadow = dropShadow;
ElementCompositionPreview.SetElementChildVisual(grid, spriteVisual);
}

感谢您的帮助。

最佳答案

这应该可以让您开始创建您正在寻找的效果。您将需要由 Microsoft 赞助的 UWP Community Toolkit 库。它具有您将在下面看到的令人惊叹的动画语法,并且它在底层使用了 Composition API。根据需要调整模糊、缩放和淡化。

XAML

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid Width="200" Height="200" HorizontalAlignment="Center" VerticalAlignment="Center" PointerEntered="Grid_PointerEntered" PointerExited="Grid_PointerExited">
<Image x:Name="imageBackdrop" Source="Assets/image.jpg" Opacity="0"/>
<Image x:Name="image" Source="Assets/image.jpg"/>
</Grid>
</Grid>

C#

using Microsoft.Toolkit.Uwp.UI.Animations;

private void Grid_PointerEntered(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
imageBackdrop.Fade(0.5f, 50).Scale(1.1f, 1.1f,0,0,0).Blur(75, 0).Offset(0, 20, 0).Start();
}

private void Grid_PointerExited(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
imageBackdrop.Opacity = 0;
}

编辑:使用容器在 XAML 中更容易扩展。还向与模糊效果相同的元素添加不透明效果似乎使模糊效果在非常低的 BlurAmount 下达到上限。将类似这样的东西与不透明蒙版或其他东西结合使用,使其变暗并羽化边缘:

private void Grid_PointerEntered(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
var element = imageBackdrop;
var visual = ElementCompositionPreview.GetElementVisual(element);
var compositor = visual.Compositor;

var effect = new GaussianBlurEffect()
{
Name = "Blur",
Source = new CompositionEffectSourceParameter("EffectSource"),
BlurAmount = 50f,
BorderMode = EffectBorderMode.Soft,
};

var blurEffectFactory = compositor.CreateEffectFactory(effect, new[] { effect.Name + "." + nameof(effect.BlurAmount) });
var brush = blurEffectFactory.CreateBrush();
var destinationBrush = compositor.CreateBackdropBrush();
brush.SetSourceParameter("EffectSource", destinationBrush);

var sprite = compositor.CreateSpriteVisual();
sprite.Size = new Vector2((float)(element.RenderSize.Width), (float)(element.RenderSize.Height));
sprite.Brush = brush;
ElementCompositionPreview.SetElementChildVisual(element, sprite);
imageBackdropContainer.Opacity = 1;
}

private void Grid_PointerExited(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
imageBackdropContainer.Opacity = 0;
}

enter image description here

关于c# - 带图像模糊 UWP 的投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49718377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com