C# 如何在Windows10Universal应用程序中制作磨砂玻璃效果?
那么,如何使用C#&XAML实现这种效果呢?C# 如何在Windows10Universal应用程序中制作磨砂玻璃效果?,c#,uwp,windows-store-apps,windows-10,win-universal-app,C#,Uwp,Windows Store Apps,Windows 10,Win Universal App,那么,如何使用C#&XAML实现这种效果呢? 您应该查看Win2D类以及其中的画布效果-有一个GaussianBlueffect可能会有所帮助。以下是参考资料: 对于高斯效应: 包括一些C#代码示例 另外:如果你想知道如何使用win2D,我刚刚找到了一个方便的教程(我现在就在学习:)自从iOS 7推出磨砂玻璃以来,我一直在尝试创建类似的东西。现在多亏了Win2D,在WinRT中创建类似效果要简单得多 首先,您需要从NuGet获取Win2D.uwp包 这个想法是基于图像源创建一个GaussianB
您应该查看Win2D类以及其中的画布效果-有一个GaussianBlueffect可能会有所帮助。以下是参考资料: 对于高斯效应: 包括一些C#代码示例
另外:如果你想知道如何使用win2D,我刚刚找到了一个方便的教程(我现在就在学习:)自从iOS 7推出磨砂玻璃以来,我一直在尝试创建类似的东西。现在多亏了Win2D,在WinRT中创建类似效果要简单得多 首先,您需要从NuGet获取Win2D.uwp包 这个想法是基于图像源创建一个
GaussianBlueffect
,并将其放在另一个白色面具上,以模仿磨砂玻璃的外观
要准备好gaussianblueffect,您需要从Win2D库创建一个xaml控件CanvasControl
。UI结构是这样的-
<Grid x:Name="ImagePanel2" Width="356" Height="200" Margin="0,0,0,40" VerticalAlignment="Bottom">
<Image x:Name="Image2" Source="Assets/Food.jpg" Stretch="UniformToFill" />
<Grid x:Name="Overlay" ManipulationMode="TranslateX" ManipulationStarted="Overlay_ManipulationStarted" ManipulationDelta="Overlay_ManipulationDelta" ManipulationCompleted="Overlay_ManipulationCompleted" RenderTransformOrigin="0.5,0.5">
<Grid.Clip>
<RectangleGeometry x:Name="Clip" Rect="0, 0, 356, 200" />
</Grid.Clip>
<Rectangle x:Name="WhiteMask" Fill="White" />
<Xaml:CanvasControl x:Name="Canvas" CreateResources="Canvas_CreateResources" Draw="Canvas_Draw" />
</Grid>
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Frosted Glass" VerticalAlignment="Top" Foreground="#FF595959" Margin="12,12,0,0" FontWeight="Light" FontSize="26.667" FontStyle="Italic" TextLineBounds="Tight" />
</Grid>
您可以进一步调整BlurAmount
属性以及不透明度图形(0.9f
),以获得所需的准确效果
还要注意的是,将来可能会有另一种(更好的?)方法来做到这一点。如果在将来的版本中支持gaussianblueffect
,我将更新答案
您可以在此GitHub中找到当前的工作示例。我在这里附上了一张图片来展示它的样子。:)
到目前为止你试过什么吗?你尝试过的一些代码或任何东西,但都不起作用?我在互联网上寻找类似的东西,但是有一些例子模糊了图像,但没有重叠,但它们与Windows 10无关,所以我甚至不知道从哪里开始。为了了解人们对我的帮助,我在这里写了,但不是为了你写一个对蔑视的回应。可能的副本没有使用Win2D类,所以不能直接说。但是,从性能的角度来看,使用SetWindowCompositionAttribute的方法是更好还是更差?我知道它使用互操作,这可能是对Win2D的投票,但似乎也非常。。。本地人,可以这么说:)(正如你可能听到的,我自己没有测试过它,只是知道它,如果你自己没有测试过,我明天会用它进行实验,并在这里发布我的发现)例如:(对不起“在你脸上”的标志)哦,很好的决定,谢谢,但这是我需要的一半。那么,是否可以选择动态内容?所以,我们有主页和动态框架,比如这里:
overlay code,content
刚才它适用于背景图像。据我所知,这一切都取决于图像,即:\u bitmap=wait CanvasBitmap.LoadAsync(发送方,“此处”)代码>那么,我们可以发送当前帧的屏幕吗?还是有其他方法?@SnakeAce,LoadAsync
也采用irandomaccesstream
。因此,您可以使用RenderTargetBitmap
从UI上的任意UI元素
获取位图,然后将其转换为InMemoryRandomAccessStream
。查看我的更新答案。查看如何应用更高级的模糊。
void Canvas_CreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
{
args.TrackAsyncAction(CreateResourcesAsync(sender).AsAsyncAction());
}
async Task CreateResourcesAsync(CanvasControl sender)
{
// give it a little bit delay to ensure the image is load, ideally you want to Image.ImageOpened event instead
await Task.Delay(200);
using (var stream = new InMemoryRandomAccessStream())
{
// get the stream from the background image
var target = new RenderTargetBitmap();
await target.RenderAsync(this.Image2);
var pixelBuffer = await target.GetPixelsAsync();
var pixels = pixelBuffer.ToArray();
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, stream);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)target.PixelWidth, (uint)target.PixelHeight, 96, 96, pixels);
await encoder.FlushAsync();
stream.Seek(0);
// load the stream into our bitmap
_bitmap = await CanvasBitmap.LoadAsync(sender, stream);
}
}
void Canvas_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
using (var session = args.DrawingSession)
{
var blur = new GaussianBlurEffect
{
BlurAmount = 50.0f, // increase this to make it more blurry or vise versa.
//Optimization = EffectOptimization.Balanced, // default value
//BorderMode = EffectBorderMode.Soft // default value
Source = _bitmap
};
session.DrawImage(blur, new Rect(0, 0, sender.ActualWidth, sender.ActualHeight),
new Rect(0, 0, _bitmap.SizeInPixels.Width, _bitmap.SizeInPixels.Height), 0.9f);
}
}
void Overlay_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
// reset the inital with of the Rect
_x = (float)this.ImagePanel2.ActualWidth;
}
void Overlay_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
// get the movement on X axis
_x += (float)e.Delta.Translation.X;
// keep the pan within the bountry
if (_x > this.ImagePanel2.ActualWidth || _x < 0) return;
// we clip the overlay to reveal the actual image underneath
this.Clip.Rect = new Rect(0, 0, _x, this.ImagePanel2.ActualHeight);
}
void Overlay_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
// reset the clip to show the full overlay
this.Clip.Rect = new Rect(0, 0, this.ImagePanel2.ActualWidth, this.ImagePanel2.ActualHeight);
}