private void canvas_CreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
        {
            arrow = MakeDirectionIcon(sender);

            var bounds  = arrow.ComputeStrokeBounds(3);
            var outline = arrow.Stroke(2).Outline();

            var foregroundBrush = (SolidColorBrush)directionsCombo.Foreground;
            var color           = foregroundBrush.Color;

            Directions = new List <DirectionInfo>();

            foreach (CanvasTextDirection direction in Enum.GetValues(typeof(CanvasTextDirection)))
            {
                var arrowImage = new CanvasImageSource(sender, 64, 64);
                using (var ds = arrowImage.CreateDrawingSession(Colors.Transparent))
                {
                    var directionTransform = GetDirectionTransform(direction);
                    ds.Transform = directionTransform * Matrix3x2.CreateTranslation((float)(32 - bounds.Width / 2), (float)(32 - bounds.Height / 2));

                    ds.DrawGeometry(arrow, color, 1);
                }

                Directions.Add(new DirectionInfo(direction, arrowImage));
            }

            // Poke the property so that the control gets a chance to pick up the new images
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Directions"));
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CurrentDirectionIndex"));
            }
        }
        private void canvas_Draw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            var ds = args.DrawingSession;

            Rect rect = new Rect(0, 0, sender.ActualWidth, sender.ActualHeight);

            if (ThumbnailGenerator.IsDrawingThumbnail)
            {
                rect.Width = sender.ActualWidth / 2;
            }

            if (!ThumbnailGenerator.IsDrawingThumbnail)
            {
                ds.DrawText("Win2D DrawText", rect, drawingColor,
                            new CanvasTextFormat()
                {
                    FontSize            = 14,
                    HorizontalAlignment = CanvasHorizontalAlignment.Center,
                    VerticalAlignment   = CanvasVerticalAlignment.Top
                });

                DrawAlignedText(ds, rect, CanvasHorizontalAlignment.Left, CanvasVerticalAlignment.Top);
                DrawAlignedText(ds, rect, CanvasHorizontalAlignment.Right, CanvasVerticalAlignment.Top);
                DrawAlignedText(ds, rect, CanvasHorizontalAlignment.Left, CanvasVerticalAlignment.Bottom);
                DrawAlignedText(ds, rect, CanvasHorizontalAlignment.Right, CanvasVerticalAlignment.Bottom);
            }

            var screenCenter = new Size(sender.ActualWidth, sender.ActualHeight).ToVector2() / 2;

            Matrix3x2 directionTransform = GetDirectionTransform(CurrentDirection);

            var arrowBounds = arrow.ComputeStrokeBounds(4);
            var arrowCenter = new Size(arrowBounds.Width, arrowBounds.Height).ToVector2() / 2;

            float scaleAmount = (float)Math.Max(5.0, (rect.Width / arrowBounds.Width) / 4);
            var   scaleAndPositionTransform = Matrix3x2.CreateScale(scaleAmount, scaleAmount, arrowCenter) * Matrix3x2.CreateTranslation(screenCenter - arrowCenter);

            ds.Transform = directionTransform * scaleAndPositionTransform;
            ds.DrawGeometry(MakeDirectionIcon(ds), drawingColor, 4, new CanvasStrokeStyle()
            {
                TransformBehavior = CanvasStrokeTransformBehavior.Fixed
            });
        }
Esempio n. 3
0
 // Used for both baseGeometry and stretchedGeometry
 void ComputeGeometryBounds(CanvasGeometry geometry, ref winFound.Rect geometryBounds, ref winFound.Rect geometryStrokeBounds)
 {
     if (geometry != null)
     {
         geometryBounds       = geometry.ComputeBounds();
         geometryStrokeBounds = geometry.ComputeStrokeBounds(strokeWidth, strokeStyle);
     }
     else
     {
         geometryBounds       = new winFound.Rect();
         geometryStrokeBounds = new winFound.Rect();
     }
 }
            Matrix3x2 GetDirectionTransform(CanvasTextDirection direction)
            {
                var       arrowBounds        = arrow.ComputeStrokeBounds(4);
                var       arrowCenter        = new Size(arrowBounds.Width, arrowBounds.Height).ToVector2() / 2;
                Matrix3x2 directionTransform = Matrix3x2.Identity;

                switch (direction)
                {
                case CanvasTextDirection.LeftToRightThenTopToBottom:
                    directionTransform = Matrix3x2.Identity;
                    break;

                case CanvasTextDirection.RightToLeftThenTopToBottom:
                    directionTransform = Matrix3x2.CreateScale(-1, 1);
                    break;

                case CanvasTextDirection.LeftToRightThenBottomToTop:
                    directionTransform = Matrix3x2.CreateScale(1, -1);
                    break;

                case CanvasTextDirection.RightToLeftThenBottomToTop:
                    directionTransform = Matrix3x2.CreateScale(-1, -1);
                    break;

                case CanvasTextDirection.TopToBottomThenLeftToRight:
                    directionTransform = Matrix3x2.CreateRotation((float)Math.PI / 2) * Matrix3x2.CreateScale(-1, 1);
                    break;

                case CanvasTextDirection.BottomToTopThenLeftToRight:
                    directionTransform = Matrix3x2.CreateRotation((float)-Math.PI / 2);
                    break;

                case CanvasTextDirection.TopToBottomThenRightToLeft:
                    directionTransform = Matrix3x2.CreateRotation((float)Math.PI / 2);
                    break;

                case CanvasTextDirection.BottomToTopThenRightToLeft:
                    directionTransform = Matrix3x2.CreateRotation((float)-Math.PI / 2) * Matrix3x2.CreateScale(-1, 1);
                    break;
                }

                directionTransform = Matrix3x2.CreateTranslation(-arrowCenter) * directionTransform * Matrix3x2.CreateTranslation(arrowCenter);
                return(directionTransform);
            }
Esempio n. 5
0
        public static CanvasBitmap Render(ICanvasResourceCreatorWithDpi resourceCreator, TextRenderSettings settings, Size renderSize, bool clip)
        {
            Rect geometryBounds;
            ICanvasResourceCreator device;
            CanvasRenderTarget     geometryRender;
            float dpi = 96;

            if (resourceCreator == null)
            {
                device         = new CanvasDevice();
                geometryRender = new CanvasRenderTarget(device, (float)renderSize.Width, (float)renderSize.Height, dpi);
            }
            else
            {
                device         = resourceCreator;
                dpi            = resourceCreator.Dpi;
                geometryRender = new CanvasRenderTarget(resourceCreator, (float)renderSize.Width, (float)renderSize.Height);
            }

            // получаем сессию для рисования текста
            using (var geometrySession = geometryRender.CreateDrawingSession())
            {
                geometrySession.Clear(Color.FromArgb(0, 0, 0, 0)); // прозрачность

                var format = new CanvasTextFormat()
                {
                    FontSize            = settings.FontSize,
                    FontFamily          = settings.FontFamily.Source,
                    HorizontalAlignment = CanvasHorizontalAlignment.Center,
                    VerticalAlignment   = CanvasVerticalAlignment.Center,
                };

                // создаем тектовый слой
                using (CanvasTextLayout textLayout = new CanvasTextLayout(device, settings.Text, format, (float)renderSize.Width, (float)renderSize.Height))
                {
                    geometrySession.DrawTextLayout(textLayout, 0, 0, settings.TextColor);

                    CanvasGeometry    geometry = CanvasGeometry.CreateText(textLayout);
                    CanvasStrokeStyle stroke   = new CanvasStrokeStyle()
                    {
                        DashStyle = settings.OutlineTextSettings.DashStyle
                    };

                    geometryBounds = geometry.ComputeStrokeBounds(settings.OutlineTextSettings.Enabled ? settings.OutlineTextSettings.StrokeWidth : 0.0f);

                    if (settings.OutlineTextSettings.Enabled)
                    {
                        geometrySession.DrawGeometry(
                            geometry,
                            settings.OutlineTextSettings.StrokeColor,
                            settings.OutlineTextSettings.StrokeWidth,
                            stroke);
                    }
                }
            }

            CanvasBitmap bitmap = CanvasBitmap.CreateFromDirect3D11Surface(device, geometryRender, dpi);


            var computedWidth  = (float)renderSize.Width;
            var computedHeight = (float)renderSize.Height;

            Matrix3x2 clipMatrix = Matrix3x2.Identity;

            // Если требуется обрезка
            if (clip)
            {
                var shadowOffset = settings.ShadowEffectSettings.BlurRadius * 2;
                computedWidth  = (float)Math.Floor(geometryBounds.Width + shadowOffset + Math.Abs(settings.ShadowEffectSettings.TranslationX));
                computedHeight = (float)Math.Floor(geometryBounds.Height + shadowOffset + Math.Abs(settings.ShadowEffectSettings.TranslationY));

                var offsetX = -(float)Math.Truncate(geometryBounds.Left);
                var offsetY = -(float)Math.Truncate(geometryBounds.Top);

                clipMatrix = Matrix3x2.CreateTranslation(new Vector2(offsetX, offsetY));
            }
            CanvasRenderTarget resultRender;

            if (resourceCreator == null)
            {
                resultRender = new CanvasRenderTarget(device, computedWidth, computedHeight, dpi);
            }
            else
            {
                resultRender = new CanvasRenderTarget(resourceCreator, computedWidth, computedHeight);
            }

            // получаем сессию для рисования текста
            using (var resultSession = resultRender.CreateDrawingSession())
            {
                resultSession.Clear(Color.FromArgb(0, 0, 0, 0));

                var shadow = new ShadowEffect
                {
                    Source     = bitmap,
                    BlurAmount = settings.ShadowEffectSettings.BlurRadius
                };

                Transform2DEffect shadowTranslation = new Transform2DEffect
                {
                    Source          = shadow,
                    TransformMatrix = clipMatrix * Matrix3x2.CreateTranslation(new Vector2(
                                                                                   settings.ShadowEffectSettings.TranslationX,
                                                                                   settings.ShadowEffectSettings.TranslationY
                                                                                   ))
                };
                resultSession.DrawImage(shadowTranslation);

                Transform2DEffect imageTranslation = new Transform2DEffect
                {
                    Source          = bitmap,
                    TransformMatrix = clipMatrix
                };

                resultSession.DrawImage(imageTranslation);
            }

            return(CanvasBitmap.CreateFromDirect3D11Surface(device, resultRender));
        }
        private void canvas_CreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
        {
            arrow = MakeDirectionIcon(sender);

            var bounds = arrow.ComputeStrokeBounds(3);
            var outline = arrow.Stroke(2).Outline();

            var foregroundBrush = (SolidColorBrush)directionsCombo.Foreground;
            var color = foregroundBrush.Color;

            Directions = new List<DirectionInfo>();

            foreach (CanvasTextDirection direction in Enum.GetValues(typeof(CanvasTextDirection)))
            {
                var arrowImage = new CanvasImageSource(sender, 64, 64);
                using (var ds = arrowImage.CreateDrawingSession(Colors.Transparent))
                {

                    var directionTransform = GetDirectionTransform(direction);
                    ds.Transform = directionTransform * Matrix3x2.CreateTranslation((float)(32 - bounds.Width / 2), (float)(32 - bounds.Height / 2));

                    ds.DrawGeometry(arrow, color, 1);
                }

                Directions.Add(new DirectionInfo(direction, arrowImage));
            }

            // Poke the property so that the control gets a chance to pick up the new images
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Directions"));
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CurrentDirectionIndex"));
            }
        }
Esempio n. 7
0
 public Rect getBounds(float strokeWidth)
 {
     return(_cg.ComputeStrokeBounds(strokeWidth));
 }