コード例 #1
0
        public void DrawRectangle(Rect frame, Size corner, Pen pen = null, Brush brush = null)
        {
            if (corner.Width > 0 || corner.Height > 0)
            {
                using (var path = new GraphicsPath()) {
                    var xdia = corner.Width * 2;
                    var ydia = corner.Height * 2;
                    if (xdia > frame.Width)
                    {
                        xdia = frame.Width;
                    }
                    if (ydia > frame.Height)
                    {
                        ydia = frame.Height;
                    }

                    // define a corner
                    var Corner = Conversions.GetRectangleF(frame);

                    path.AddArc(Corner, 180, 90);

                    // top right
                    Corner.X += (float)(frame.Width - xdia);
                    path.AddArc(Corner, 270, 90);

                    // bottom right
                    Corner.Y += (float)(frame.Height - ydia);
                    path.AddArc(Corner, 0, 90);

                    // bottom left
                    Corner.X -= (float)(frame.Width - xdia);
                    path.AddArc(Corner, 90, 90);

                    // end path
                    path.CloseFigure();

                    if (brush != null)
                    {
                        graphics.FillPath(brush.GetBrush(frame), path);
                    }
                    if (pen != null)
                    {
                        graphics.DrawPath(pen.GetPen(), path);
                    }
                }
            }
            else
            {
                if (brush != null)
                {
                    graphics.FillRectangle(brush.GetBrush(frame), Conversions.GetRectangleF(frame));
                }
                if (pen != null)
                {
                    var r = Conversions.GetRectangleF(frame);
                    graphics.DrawRectangle(pen.GetPen(), r.X, r.Y, r.Width, r.Height);
                }
            }
        }
コード例 #2
0
        public void DrawRectangle(Rect frame, Size corner, Pen pen = null, Brush brush = null)
        {
            if (corner.Width > 0 || corner.Height > 0)
            {
                var geometry = new StreamGeometry();
                using (var context = geometry.Open())
                {
                    bool       isStroked    = pen != null;
                    const bool isSmoothJoin = true;

                    var diag         = corner.Diagonal;
                    var cornerRadius = new {
                        TopLeft     = diag,
                        TopRight    = diag,
                        BottomRight = diag,
                        BottomLeft  = diag
                    };

                    var rect = frame.GetRect();

                    context.BeginFigure(rect.TopLeft + new Vector(0, cornerRadius.TopLeft), brush != null, true);
                    context.ArcTo(new System.Windows.Point(rect.TopLeft.X + cornerRadius.TopLeft, rect.TopLeft.Y),
                                  new System.Windows.Size(cornerRadius.TopLeft, cornerRadius.TopLeft),
                                  90, false, SweepDirection.Clockwise, isStroked, isSmoothJoin);

                    context.LineTo(rect.TopRight - new Vector(cornerRadius.TopRight, 0), isStroked, isSmoothJoin);
                    context.ArcTo(new System.Windows.Point(rect.TopRight.X, rect.TopRight.Y + cornerRadius.TopRight),
                                  new System.Windows.Size(cornerRadius.TopRight, cornerRadius.TopRight),
                                  90, false, SweepDirection.Clockwise, isStroked, isSmoothJoin);

                    context.LineTo(rect.BottomRight - new Vector(0, cornerRadius.BottomRight), isStroked, isSmoothJoin);
                    context.ArcTo(new System.Windows.Point(rect.BottomRight.X - cornerRadius.BottomRight, rect.BottomRight.Y),
                                  new System.Windows.Size(cornerRadius.BottomRight, cornerRadius.BottomRight),
                                  90, false, SweepDirection.Clockwise, isStroked, isSmoothJoin);

                    context.LineTo(rect.BottomLeft + new Vector(cornerRadius.BottomLeft, 0), isStroked, isSmoothJoin);
                    context.ArcTo(new System.Windows.Point(rect.BottomLeft.X, rect.BottomLeft.Y - cornerRadius.BottomLeft),
                                  new System.Windows.Size(cornerRadius.BottomLeft, cornerRadius.BottomLeft),
                                  90, false, SweepDirection.Clockwise, isStroked, isSmoothJoin);

                    context.Close();
                }
                dc.DrawGeometry(brush?.GetBrush(), pen?.GetPen(), geometry);
            }
            else
            {
                dc.DrawRectangle(brush?.GetBrush(), pen?.GetPen(), frame.GetRect());
            }
        }
コード例 #3
0
        public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null)
        {
            var formattedText = new FormattedText(text, CultureInfo.InvariantCulture, System.Windows.FlowDirection.LeftToRight, font.GetTypeface(), font.Size, null);

            formattedText.SetForegroundBrush(brush?.GetBrush());
            dc.DrawText(formattedText, frame.BottomLeft.GetPoint());
        }
コード例 #4
0
 public void DrawEllipse(Rect frame, Pen pen = null, Brush brush = null)
 {
     if (brush != null)
     {
         graphics.FillEllipse(brush.GetBrush(frame), Conversions.GetRectangleF(frame));
     }
     if (pen != null)
     {
         graphics.DrawEllipse(pen.GetPen(), Conversions.GetRectangleF(frame));
     }
 }
コード例 #5
0
 public void DrawRectangle(Rect frame, Pen pen = null, Brush brush = null)
 {
     if (brush != null)
     {
         graphics.FillRectangle(brush.GetBrush(frame), Conversions.GetRectangleF(frame));
     }
     if (pen != null)
     {
         var r = Conversions.GetRectangleF(frame);
         graphics.DrawRectangle(pen.GetPen(), r.X, r.Y, r.Width, r.Height);
     }
 }
コード例 #6
0
        public void DrawPath(IEnumerable <PathOp> ops, Pen pen, Brush brush)
        {
            StreamGeometry streamGeometry = new StreamGeometry();

            using (StreamGeometryContext geometryContext = streamGeometry.Open())
            {
                foreach (var op in ops)
                {
                    var mt = op as MoveTo;
                    if (mt != null)
                    {
                        geometryContext.BeginFigure(mt.Point.GetPoint(), true, false);
                        continue;
                    }

                    var lt = op as LineTo;
                    if (lt != null)
                    {
                        geometryContext.LineTo(lt.Point.GetPoint(), true, false);
                        continue;
                    }

                    var at = op as ArcTo;
                    if (at != null)
                    {
                        /*
                         * Point c1, c2;
                         * at.GetCircles(pp, out c1, out c2);
                         * var circleCenter = at.LargeArc ^ !at.SweepClockwise ? c2 : c1;
                         * var rotationAngle = (float)Math.Atan2(at.Point.Y - circleCenter.Y, at.Point.X - circleCenter.X);
                         * geometryContext.ArcTo(at.Point.GetPoint(), at.Radius.GetSize(), rotationAngle,
                         *  at.LargeArc, at.SweepClockwise ? SweepDirection.Clockwise : SweepDirection.Counterclockwise,
                         *  true, false
                         * );*/
                        throw new NotImplementedException();
                    }

                    var ct = op as CurveTo;
                    if (ct != null)
                    {
                        geometryContext.BezierTo(ct.Control1.GetPoint(), ct.Control2.GetPoint(), ct.Point.GetPoint(), true, false);
                    }

                    throw new NotSupportedException();
                }
            }

            dc.DrawGeometry(brush?.GetBrush(), pen?.GetPen(), streamGeometry);
        }
コード例 #7
0
 public void DrawEllipse(Rect frame, Pen pen = null, Brush brush = null)
 {
     dc.DrawEllipse(brush?.GetBrush(), pen?.GetPen(), frame.Center.GetPoint(), frame.Width / 2, frame.Height / 2);
 }
コード例 #8
0
 public void DrawRectangle(Rect frame, Pen pen = null, Brush brush = null)
 {
     dc.DrawRectangle(brush?.GetBrush(), pen?.GetPen(), frame.GetRect());
 }
コード例 #9
0
        public void DrawPath(IEnumerable <PathOp> ops, Pen pen = null, Brush brush = null)
        {
            using (var path = new GraphicsPath()) {
                var bb = new BoundingBoxBuilder();

                var position = Point.Zero;

                foreach (var op in ops)
                {
                    var mt = op as MoveTo;
                    if (mt != null)
                    {
                        var p = mt.Point;
                        position = p;
                        bb.Add(p);
                        continue;
                    }
                    var lt = op as LineTo;
                    if (lt != null)
                    {
                        var p = lt.Point;
                        path.AddLine(Conversions.GetPointF(position), Conversions.GetPointF(p));
                        position = p;
                        bb.Add(p);
                        continue;
                    }
                    var at = op as ArcTo;
                    if (at != null)
                    {
                        var p = at.Point;
                        path.AddLine(Conversions.GetPointF(position), Conversions.GetPointF(p));
                        position = p;
                        bb.Add(p);
                        continue;
                    }
                    var ct = op as CurveTo;
                    if (ct != null)
                    {
                        var p  = ct.Point;
                        var c1 = ct.Control1;
                        var c2 = ct.Control2;
                        path.AddBezier(Conversions.GetPointF(position), Conversions.GetPointF(c1),
                                       Conversions.GetPointF(c2), Conversions.GetPointF(p));
                        position = p;
                        bb.Add(p);
                        bb.Add(c1);
                        bb.Add(c2);
                        continue;
                    }
                    var cp = op as ClosePath;
                    if (cp != null)
                    {
                        path.CloseFigure();
                        continue;
                    }

                    throw new NotSupportedException("Path Op " + op);
                }

                var frame = bb.BoundingBox;
                if (brush != null)
                {
                    graphics.FillPath(brush.GetBrush(frame), path);
                }
                if (pen != null)
                {
                    graphics.DrawPath(pen.GetPen(), path);
                }
            }
        }
コード例 #10
0
        public void DrawPath(IEnumerable <PathOp> ops, Pen pen = null, Brush brush = null)
        {
            using (var path = new GraphicsPath()) {
                var bb = new BoundingBoxBuilder();

                var position = Point.Zero;

                foreach (var op in ops)
                {
                    var mt = op as MoveTo;
                    if (mt != null)
                    {
                        var p = mt.Point;
                        position = p;
                        bb.Add(p);
                        continue;
                    }
                    var lt = op as LineTo;
                    if (lt != null)
                    {
                        var p = lt.Point;
                        path.AddLine(Conversions.GetPointF(position), Conversions.GetPointF(p));
                        position = p;
                        bb.Add(p);
                        continue;
                    }
                    var at = op as ArcTo;
                    if (at != null)
                    {
                        var p = at.Point;

                        Point c1, c2;
                        at.GetCircles(position, out c1, out c2);

                        var circleCenter = (at.LargeArc ^ at.SweepClockwise) ? c1 : c2;
                        var rect         = new Rect(circleCenter - at.Radius, at.Radius * 2);

                        var startAngle = Conversions.RadToDeg((float)Math.Atan2(position.Y - circleCenter.Y, position.X - circleCenter.X));
                        var endAngle   = Conversions.RadToDeg((float)Math.Atan2(p.Y - circleCenter.Y, p.X - circleCenter.X));

                        var sweepAngle = endAngle - startAngle;

                        if (at.SweepClockwise && sweepAngle < 0)
                        {
                            // If we want to go CW, sweepAngle needs to be positive
                            sweepAngle += 360.0f;
                        }
                        else if (!at.SweepClockwise && sweepAngle > 0)
                        {
                            // If we want to go CCW, sweepAngle needs to be negative
                            sweepAngle -= 360.0f;
                        }

                        path.AddArc(Conversions.GetRectangleF(rect), startAngle, sweepAngle);
                        position = p;
                        bb.Add(p);
                        continue;
                    }
                    var ct = op as CurveTo;
                    if (ct != null)
                    {
                        var p  = ct.Point;
                        var c1 = ct.Control1;
                        var c2 = ct.Control2;
                        path.AddBezier(Conversions.GetPointF(position), Conversions.GetPointF(c1),
                                       Conversions.GetPointF(c2), Conversions.GetPointF(p));
                        position = p;
                        bb.Add(p);
                        bb.Add(c1);
                        bb.Add(c2);
                        continue;
                    }
                    var cp = op as ClosePath;
                    if (cp != null)
                    {
                        path.CloseFigure();
                        continue;
                    }

                    throw new NotSupportedException("Path Op " + op);
                }

                var frame = bb.BoundingBox;
                if (brush != null)
                {
                    graphics.FillPath(brush.GetBrush(frame), path);
                }
                if (pen != null)
                {
                    graphics.DrawPath(pen.GetPen(), path);
                }
            }
        }