当前位置: 首页>>代码示例>>C#>>正文


C# CanvasRenderTarget.GetPixelBytes方法代码示例

本文整理汇总了C#中CanvasRenderTarget.GetPixelBytes方法的典型用法代码示例。如果您正苦于以下问题:C# CanvasRenderTarget.GetPixelBytes方法的具体用法?C# CanvasRenderTarget.GetPixelBytes怎么用?C# CanvasRenderTarget.GetPixelBytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CanvasRenderTarget的用法示例。


在下文中一共展示了CanvasRenderTarget.GetPixelBytes方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DrawEffect

        public async void DrawEffect()
        {
            if (Target == null)
            {
                return;
            }

            if (MainContainer != null)
            {
                foreach (var child in MainContainer.Children)
                {
                    if (child is SpriteVisual)
                    {
                        (child as SpriteVisual).Brush.Dispose();
                    }
                }
                RemoveEffect();
            }

            RenderTargetBitmap bitmap = new RenderTargetBitmap();
            await bitmap.RenderAsync(Target);
            var pixels  = (await bitmap.GetPixelsAsync()).ToArray();
            
            Size srcSize = new Size(bitmap.PixelWidth, bitmap.PixelHeight);
            Size decSize = new Size((int)BlurAmount * 10 + bitmap.PixelWidth, (int)BlurAmount * 10 + bitmap.PixelHeight);
            Size blurSize = new Size(decSize.Width - srcSize.Width, decSize.Height - srcSize.Height);
            Point transform = new Point(blurSize.Width/2, blurSize.Height/2);
            transform.X += Math.Cos(Direction / 360 * 2 * Math.PI) * Depth;
            transform.Y += Math.Sin(Direction / 360 * 2 * Math.PI) * Depth;
            
            ContainerVisual visual = Target.GetVisual() as ContainerVisual;

            Compositor compositor = visual.Compositor;

            CanvasDevice device = CanvasDevice.GetSharedDevice();
            CanvasRenderTarget offscreen = new CanvasRenderTarget(device, (int)decSize.Width, (int)decSize.Height, 96);

            using (CanvasDrawingSession ds = offscreen.CreateDrawingSession())
            {
                Transform2DEffect finalEffect = new Transform2DEffect()
                {
                    Source = new Microsoft.Graphics.Canvas.Effects.ShadowEffect()
                    {
                        Source = CanvasBitmap.CreateFromBytes(ds, pixels, (int)srcSize.Width, (int)srcSize.Height,
                                DirectXPixelFormat.B8G8R8A8UIntNormalized),
                        BlurAmount = (float)BlurAmount,
                        ShadowColor = ShadowColor
                    },
                    TransformMatrix = Matrix3x2.CreateTranslation((float)blurSize.Width / 2, (float)blurSize.Height / 2)
                };


                ds.DrawImage(finalEffect);
            }

            var effectPixels = offscreen.GetPixelBytes();

            var imageFactory =
                Microsoft.UI.Composition.Toolkit.CompositionImageFactory.CreateCompositionImageFactory(compositor);

            var effectImage = imageFactory.CreateImageFromPixels(effectPixels, (int)decSize.Width, (int)decSize.Height);
            var effectbrush = compositor.CreateSurfaceBrush(effectImage.Surface);
            var effectVisual = compositor.CreateSpriteVisual();

            effectVisual.Brush = effectbrush;
            effectVisual.Offset = new Vector3( -(float)transform.X, -(float)transform.Y, 0);
            effectVisual.Size = new Vector2((float)decSize.Width, (float)decSize.Height);

            var srcImage = imageFactory.CreateImageFromPixels(pixels, (int)srcSize.Width, (int)srcSize.Height);
            var srcbrush = compositor.CreateSurfaceBrush(srcImage.Surface);
            var srcVisual = compositor.CreateSpriteVisual();

            srcVisual.Brush = srcbrush;
            srcVisual.Offset = new Vector3(0, 0, 0);
            srcVisual.Size = new Vector2((int)srcSize.Width, (int)srcSize.Height);

            if (MainContainer == null)
            {
                MainContainer = compositor.CreateContainerVisual();
                ElementCompositionPreview.SetElementChildVisual(Target, MainContainer);
            }

            MainContainer.Children.InsertAtTop(srcVisual);
            MainContainer.Children.InsertAtBottom(effectVisual);
        }
开发者ID:x01673,项目名称:CompositionHelper,代码行数:85,代码来源:ShadowEffect.cs


注:本文中的CanvasRenderTarget.GetPixelBytes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。