如何对UWP应用程序应用live gaussain模糊效果?

时间:2015-11-08 03:12:43

标签: c# uwp win2d

最近我正在学习UWP并尝试构建一个简单的演示,我只想生成像Aero这样的简单实时模糊效果。它有时很有用(例如,实时相机滤镜),所以我提出了这样的想法:

首先我导入了win2D库并在RootGrid上放置了 CanvasAnimatedControl

<canvas:CanvasAnimatedControl x:Name="BlurLayer" Draw="BlurLayer_Draw" 
                              VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>

此模糊图层的资源是框架

<Frame x:Name="MainFrame" LayoutUpdated="MainFrame_LayoutUpdated" 
       VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>

每次框架更新时, MainFrame_LayoutUpdated 方法尝试保存快捷方式使用RenderTargetBitmap,但是,win2D无法接收此对象作为其CanvasBitmap,因此将其转换为byte [ ]并保存;

    RenderTargetBitmap renderer = new RenderTargetBitmap();
    CanvasBitmap bitmap;
    byte[] RendererStream;
    public static int FrameWidth;
    public static int FrameHeight;
    public ICanvasImage RenderFinal;
    public bool Frame_Updated = false;
private async void MainFrame_LayoutUpdated(object sender, object e)
    {
        await renderer.RenderAsync(MainFrame);
        FrameWidth = renderer.PixelWidth;
        FrameHeight = renderer.PixelHeight;
        RendererStream = WindowsRuntimeBufferExtensions.ToArray(await renderer.GetPixelsAsync());
        if (FrameHeight != 0)
            Frame_Updated = true;
    }

BlurLayer_Draw 方法中,只需刷新并应用模糊效果: private void BlurLayer_Draw(ICanvasAnimatedControl sender,

CanvasAnimatedDrawEventArgs args)
        {
            if (Frame_Updated)
            {
                bitmap = CanvasBitmap.CreateFromBytes(sender, RendererStream, FrameWidth, FrameHeight, DirectXPixelFormat.B8G8R8A8UIntNormalized);
                RenderFinal = new GaussianBlurEffect
                                 {
                                      Source = bitmap
                                 };
                RenderFinal.BorderMode = EffectBorderMode.Hard;
                RenderFinal.BlurAmount = 8.0f;
                Frame_Updated = false;
            }
            if (RenderFinal != null)
                args.DrawingSession.DrawImage(RenderFinal);
        }

当这一切完成后,它就有效了。 但这个解决方案非常糟糕,win2D Canvas被严重推迟了。 那么,我该怎么做才能使它具有高性能呢?

2 个答案:

答案 0 :(得分:1)

正式回答我的评论:使用Lumia Imaging SDK。如果使用SwapChainPanelRenderer进行实时渲染,它的BlurEffect非常快。我在我的博客文章Using SwapChainPanelRenderer to improve real-time rendering in Lumia Imaging SDK 3中写了这篇文章,其中我使用了BlurEffect作为示例。完整的源代码是available on GitHub

只要模糊KernelSize值更改为新的滑块值,就会调用<?php while($row = mysql_fetch_assoc($q)){ echo "<tbody>"; echo "<tr>"; echo "<td><input type = 'checkbox' name = 'checkbox[]' value='".$row['student_id']."' ></td>"; echo "<td>".$row['student_id']."</td>"; echo "<td>".$row['fname']."</td>"; echo "<td>".$row['lname']."</td>"; echo "<td>".$row['yearlevel']."</td>"; echo "</tr>"; echo "</tbody>"; } ?> 实例上的RenderAsync

SwapChainPanelRenderer

答案 1 :(得分:0)

如果您需要,还可以使用带有Windows 10周年更新的Composition API实现此效果。我写了一个类似问题的答案here