人人人人人人人人人人人人

Win2D 投影效果 ShadowEffect

 

<Page
    x:Class="Win2DDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Win2DDemo"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:canvas="using:Microsoft.Graphics.Canvas.UI.Xaml"
                   
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    Unloaded="Page_Unloaded"
    
    >

    <Grid x:Name="root">
        <canvas:CanvasControl x:Name="canvasControl"  CreateResources="CanvasControl_CreateResources" Draw="canvasControl_Draw" ClearColor="CornflowerBlue" />
        <!--<controls2:DropShadowPanel Color="Black"
                          OffsetX="-50"
                          OffsetY="-50"
                          BlurRadius="50"
                          ShadowOpacity=".7"
                          Width="120"
                          Height="120"
                          HorizontalContentAlignment="Stretch"
                          VerticalContentAlignment="Stretch">
            <Ellipse />
        </controls2:DropShadowPanel>-->
  
    
    </Grid>
</Page>

  

using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Effects;
using Microsoft.Graphics.Canvas.UI.Xaml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace Win2DDemo
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }


        CanvasBitmap bitmap;
        void canvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            //CanvasBitmap bitmap =await  CanvasBitmap.LoadAsync(sender, @"Assets\LockScreenLogo.scale-200.png");
            //实例化阴影特效
            ShadowEffect shadowEffect = new ShadowEffect
            {
                Source = bitmap,
                BlurAmount = 60.0f,
                ShadowColor = Colors.Black,
            };
            //实例化变换特效
            Transform2DEffect transform2DEffect = new Transform2DEffect
            {
                Source = shadowEffect,
                TransformMatrix = Matrix3x2.CreateTranslation(new Vector2(10, 10)),
            };
            //实例化透明特效
            OpacityEffect opacityEffect = new OpacityEffect
            {
                Source = transform2DEffect,
                Opacity = 0.82f,
            };


            //实例化结合特效
            CompositeEffect effect2 = new CompositeEffect
            {
                Sources ={
                      opacityEffect ,
                      bitmap,
                    }
            };

            args.DrawingSession.DrawImage(effect2, new Vector2(111,111));

            //CanvasCommandList cl = new CanvasCommandList(sender);
            //using (CanvasDrawingSession clds = cl.CreateDrawingSession())
            //{
            //    clds.FillRectangle(new Rect(100, 100, 100, 100), Colors.White);
            //    clds.DrawImage(bitmap, new Rect(10, 10, 100, 100));
            //}

            //ShadowEffect effect = new ShadowEffect()
            //{
            //    Source = cl,
            //    BlurAmount = 0.5f,
            //   //ShadowColor = Colors.Black,
            //};
            //args.DrawingSession.DrawImage(effect);
        }


        private void Page_Unloaded(object sender, RoutedEventArgs e)
        { //Microsoft.Toolkit.Uwp.UI.Controls.DropShadowPanel 

            if (this.canvasControl != null)
            {
                this.canvasControl.RemoveFromVisualTree();
                this.canvasControl = null;
            }
        }

        private void CanvasControl_CreateResources(CanvasControl sender, Microsoft.Graphics.Canvas.UI.CanvasCreateResourcesEventArgs args)
        {

            Task task = this.CreateResourcesAsync(sender);
            IAsyncAction action = task.AsAsyncAction();
            args.TrackAsyncAction(action);
        }
        private async Task CreateResourcesAsync(CanvasControl sender)
        {
            bitmap = await CanvasBitmap.LoadAsync(sender, "Assets/1.png");
        }




    }
}

  

 

另外Microsoft.Toolkit.Uwp.UI.Controls 里 的  DropShadowPanel 也行,但是不好用感觉。

 

posted @ 2021-04-09 16:58  wgscd  阅读(30)  评论(0编辑  收藏  举报
test red font.