/// <summary> /// 現在の図形に2次ベジエ曲線を追加します /// </summary> /// <param name="graphicsPath"></param> /// <param name="begin"></param> /// <param name="end"></param> /// <param name="anchor"></param> public static void AddBezier(this GraphicsPath graphicsPath, PointF begin, PointF anchor, PointF end) { float ratio = 2 / 3f; PointF beginAnchor = begin.Add(anchor.Sub(begin).Mult(ratio)); PointF endAnchor = end.Add(anchor.Sub(end).Mult(ratio)); graphicsPath?.AddBezier(begin, beginAnchor, endAnchor, end); }
private GraphicsPath RoundRect(RectangleF r, float r1, float r2, float r3, float r4) { float x = r.X, y = r.Y, w = r.Width, h = r.Height; GraphicsPath rr = new GraphicsPath(); rr.AddBezier(x, y + r1, x, y, x + r1, y, x + r1, y); rr.AddLine(x + r1, y, x + w - r2, y); rr.AddBezier(x + w - r2, y, x + w, y, x + w, y + r2, x + w, y + r2); rr.AddLine(x + w, y + r2, x + w, y + h - r3); rr.AddBezier(x + w, y + h - r3, x + w, y + h, x + w - r3, y + h, x + w - r3, y + h); rr.AddLine(x + w - r3, y + h, x + r4, y + h); rr.AddBezier(x + r4, y + h, x, y + h, x, y + h - r4, x, y + h - r4); rr.AddLine(x, y + h - r4, x, y + r1); return rr; }
public override void AddToPath(GraphicsPath graphicsPath) { if (this.Start == this.End) { return; } if (this.RadiusX == 0.0f && this.RadiusY == 0.0f) { graphicsPath.AddLine(this.Start, this.End); return; } double sinPhi = Math.Sin(this.Angle * SvgArcSegment.RadiansPerDegree); double cosPhi = Math.Cos(this.Angle * SvgArcSegment.RadiansPerDegree); double x1dash = cosPhi * (this.Start.X - this.End.X) / 2.0 + sinPhi * (this.Start.Y - this.End.Y) / 2.0; double y1dash = -sinPhi * (this.Start.X - this.End.X) / 2.0 + cosPhi * (this.Start.Y - this.End.Y) / 2.0; double root; double numerator = this.RadiusX * this.RadiusX * this.RadiusY * this.RadiusY - this.RadiusX * this.RadiusX * y1dash * y1dash - this.RadiusY * this.RadiusY * x1dash * x1dash; float rx = this.RadiusX; float ry = this.RadiusY; if (numerator < 0.0) { float s = (float)Math.Sqrt(1.0 - numerator / (this.RadiusX * this.RadiusX * this.RadiusY * this.RadiusY)); rx *= s; ry *= s; root = 0.0; } else { root = ((this.Size == SvgArcSize.Large && this.Sweep == SvgArcSweep.Positive) || (this.Size == SvgArcSize.Small && this.Sweep == SvgArcSweep.Negative) ? -1.0 : 1.0) * Math.Sqrt(numerator / (this.RadiusX * this.RadiusX * y1dash * y1dash + this.RadiusY * this.RadiusY * x1dash * x1dash)); } double cxdash = root * rx * y1dash / ry; double cydash = -root * ry * x1dash / rx; double cx = cosPhi * cxdash - sinPhi * cydash + (this.Start.X + this.End.X) / 2.0; double cy = sinPhi * cxdash + cosPhi * cydash + (this.Start.Y + this.End.Y) / 2.0; double theta1 = SvgArcSegment.CalculateVectorAngle(1.0, 0.0, (x1dash - cxdash) / rx, (y1dash - cydash) / ry); double dtheta = SvgArcSegment.CalculateVectorAngle((x1dash - cxdash) / rx, (y1dash - cydash) / ry, (-x1dash - cxdash) / rx, (-y1dash - cydash) / ry); if (this.Sweep == SvgArcSweep.Negative && dtheta > 0) { dtheta -= 2.0 * Math.PI; } else if (this.Sweep == SvgArcSweep.Positive && dtheta < 0) { dtheta += 2.0 * Math.PI; } int segments = (int)Math.Ceiling((double)Math.Abs(dtheta / (Math.PI / 2.0))); double delta = dtheta / segments; double t = 8.0 / 3.0 * Math.Sin(delta / 4.0) * Math.Sin(delta / 4.0) / Math.Sin(delta / 2.0); double startX = this.Start.X; double startY = this.Start.Y; for (int i = 0; i < segments; ++i) { double cosTheta1 = Math.Cos(theta1); double sinTheta1 = Math.Sin(theta1); double theta2 = theta1 + delta; double cosTheta2 = Math.Cos(theta2); double sinTheta2 = Math.Sin(theta2); double endpointX = cosPhi * rx * cosTheta2 - sinPhi * ry * sinTheta2 + cx; double endpointY = sinPhi * rx * cosTheta2 + cosPhi * ry * sinTheta2 + cy; double dx1 = t * (-cosPhi * rx * sinTheta1 - sinPhi * ry * cosTheta1); double dy1 = t * (-sinPhi * rx * sinTheta1 + cosPhi * ry * cosTheta1); double dxe = t * (cosPhi * rx * sinTheta2 + sinPhi * ry * cosTheta2); double dye = t * (sinPhi * rx * sinTheta2 - cosPhi * ry * cosTheta2); graphicsPath.AddBezier((float)startX, (float)startY, (float)(startX + dx1), (float)(startY + dy1), (float)(endpointX + dxe), (float)(endpointY + dye), (float)endpointX, (float)endpointY); theta1 = theta2; startX = (float)endpointX; startY = (float)endpointY; } }
public void AddBezier(Point pt1, Point pt2, Point pt3, Point pt4) { Changed(); gdiPath.AddBezier(pt1, pt2, pt3, pt4); }
public static SharpDX.Direct2D1.Bitmap RedrawCommonNoteBitmapCache(RenderTarget renderTargetSource, Color color, bool flipVertical, bool hollow) { ImagingFactory factory = new ImagingFactory(); SharpDX.Direct2D1.Factory factory2 = new SharpDX.Direct2D1.Factory(); SharpDX.DirectWrite.Factory factory3 = new SharpDX.DirectWrite.Factory(); SharpDX.WIC.Bitmap wicBitmap = new SharpDX.WIC.Bitmap(factory, 0x10, 0x10, SharpDX.WIC.PixelFormat.Format32bppPBGRA, BitmapCreateCacheOption.CacheOnDemand); RenderTargetProperties renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Unknown), 0f, 0f, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); WicRenderTarget renderTarget = new WicRenderTarget(factory2, wicBitmap, renderTargetProperties); try { PointF[] pathPoints; renderTarget.BeginDraw(); renderTarget.Clear(new RawColor4?(Color.Transparent.ToRawColor4(1f))); RawVector2 vector = new RawVector2(8f, 8f); using (GraphicsPath path = new GraphicsPath()) { int num = flipVertical ? -1 : 1; float num2 = vector.X - 6f; float y = vector.Y; path.AddBezier(num2, y, num2 + 2f, y - (6 * num), (num2 + 12f) + 2f, y - (6 * num), num2 + 12f, y); path.AddBezier(num2 + 12f, y, (num2 + 12f) - 2f, y + (6 * num), num2 - 2f, y + (6 * num), num2, y); if (hollow) { path.AddLine(num2, y, num2 + 4f, y); path.AddBezier(num2 + 4f, y, num2 + 5f, y - (2 * num), (num2 + 12f) + 0f, y - (6 * num), (num2 + 12f) - 4f, y); path.AddBezier((num2 + 12f) - 4f, y, (num2 + 12f) - 5f, y + (2 * num), num2 - 0f, y + (6 * num), num2 + 4f, y); path.AddLine(num2 + 4f, y, num2, y); } path.Flatten(); pathPoints = path.PathPoints; path.Dispose(); } PathGeometry geometry = new PathGeometry(factory2); if (pathPoints.Length > 1) { GeometrySink sink = geometry.Open(); sink.SetSegmentFlags(PathSegment.ForceRoundLineJoin); sink.BeginFigure(new RawVector2(pathPoints[0].X, pathPoints[0].Y), FigureBegin.Filled); for (int i = 1; i < pathPoints.Length; i++) { sink.AddLine(new RawVector2(pathPoints[i].X, pathPoints[i].Y)); } sink.EndFigure(FigureEnd.Closed); sink.Close(); sink.Dispose(); } SolidColorBrush brush = new SolidColorBrush(renderTarget, color.ToRawColor4(1f)); renderTarget.FillGeometry(geometry, brush); brush.Dispose(); geometry.Dispose(); renderTarget.EndDraw(); } catch (Exception) { factory.Dispose(); factory2.Dispose(); factory3.Dispose(); wicBitmap.Dispose(); renderTarget.Dispose(); return(null); } SharpDX.Direct2D1.Bitmap bitmap2 = SharpDX.Direct2D1.Bitmap.FromWicBitmap(renderTargetSource, wicBitmap); factory.Dispose(); factory2.Dispose(); factory3.Dispose(); wicBitmap.Dispose(); renderTarget.Dispose(); return(bitmap2); }
public static void DrawTab(Graphics g, Rectangle r, Corners corner, GradientType gradient, Color darkColor, Color lightColor, Color edgeColor, bool closed) { //dims Point[] points; GraphicsPath path; Region region; LinearGradientBrush linearBrush; Brush brush = null; Pen pen; //set brushes switch (gradient) { case GradientType.Flat: brush = new SolidBrush(darkColor); break; case GradientType.Linear: brush = new LinearGradientBrush(r, darkColor, lightColor, LinearGradientMode.Vertical); break; case GradientType.Bell: linearBrush = new LinearGradientBrush(r, darkColor, lightColor, LinearGradientMode.Vertical); linearBrush.SetSigmaBellShape(0.17F, 0.67F); brush = linearBrush; break; } pen = new Pen(edgeColor, 1); //generic points points = new Point[12] { new Point(r.Left, r.Bottom), //0 new Point(r.Left, r.Bottom - bshift), //1 new Point(r.Left, r.Top + bshift), //2 new Point(r.Left, r.Top), //3 new Point(r.Left + bshift, r.Top), //4 new Point(r.Right - bshift, r.Top), //5 new Point(r.Right, r.Top), //6 new Point(r.Right, r.Top + bshift), //7 new Point(r.Right, r.Bottom - bshift), //8 new Point(r.Right, r.Bottom), //9 new Point(r.Right - bshift, r.Bottom), //10 new Point(r.Left + bshift, r.Bottom) //10 }; path = new GraphicsPath(); switch (corner) { case Corners.LB: path.AddLine(points[3], points[1]); path.AddBezier(points[1], points[0], points[0], points[11]); path.AddLine(points[11], points[9]); path.AddLine(points[9], points[6]); path.AddLine(points[6], points[3]); region = new Region(path); g.FillRegion(brush, region); g.DrawLine(pen, points[3], points[1]); g.DrawBezier(pen, points[1], points[0], points[0], points[11]); g.DrawLine(pen, points[11], points[9]); g.DrawLine(pen, points[9], points[6]); if (closed) { g.DrawLine(pen, points[6], points[3]); } break; case Corners.LT: path.AddLine(points[0], points[2]); path.AddBezier(points[2], points[3], points[3], points[4]); path.AddLine(points[4], points[6]); path.AddLine(points[6], points[9]); path.AddLine(points[9], points[0]); region = new Region(path); g.FillRegion(brush, region); g.DrawLine(pen, points[0], points[2]); g.DrawBezier(pen, points[2], points[3], points[3], points[4]); g.DrawLine(pen, points[4], points[6]); g.DrawLine(pen, points[6], points[9]); if (closed) { g.DrawLine(pen, points[9], points[0]); } break; case Corners.RB: path.AddLine(points[3], points[0]); path.AddLine(points[0], points[10]); path.AddBezier(points[10], points[9], points[9], points[8]); path.AddLine(points[8], points[6]); path.AddLine(points[6], points[3]); region = new Region(path); g.FillRegion(brush, region); g.DrawLine(pen, points[3], points[0]); g.DrawLine(pen, points[0], points[10]); g.DrawBezier(pen, points[10], points[9], points[9], points[8]); g.DrawLine(pen, points[8], points[6]); if (closed) { g.DrawLine(pen, points[6], points[3]); } break; case Corners.RT: path.AddLine(points[0], points[3]); path.AddLine(points[3], points[5]); path.AddBezier(points[5], points[6], points[6], points[7]); path.AddLine(points[7], points[9]); path.AddLine(points[9], points[0]); region = new Region(path); g.FillRegion(brush, region); g.DrawLine(pen, points[0], points[3]); g.DrawLine(pen, points[3], points[5]); g.DrawBezier(pen, points[5], points[6], points[6], points[7]); g.DrawLine(pen, points[7], points[9]); if (closed) { g.DrawLine(pen, points[9], points[0]); } break; } }
public GraphicsPath BuildTitleBarButtonsBox(Rectangle r) { GraphicsPath XButtonBox = new GraphicsPath(); switch (m_eTitleBarType) { case GradualTitleBarType.Rounded: XButtonBox.AddBezier( new Point(r.Left - 5, r.Top + 1), new Point(r.Left + 20, r.Top + 5), new Point(r.Left - 5, r.Bottom + 2), new Point(r.Left + 45, r.Bottom)); XButtonBox.AddLine( r.Left + 45, r.Bottom, r.Right - 5, r.Bottom); XButtonBox.AddBezier( new Point(r.Right - 5, r.Bottom), new Point(r.Right - 2, r.Bottom - 1), new Point(r.Right - 2, r.Bottom - 1), new Point(r.Right, r.Bottom - 5)); XButtonBox.AddLine( r.Right, r.Bottom - 5, r.Right, r.Top + 1); break; case GradualTitleBarType.Angular: XButtonBox.AddLine( r.Left + 18, r.Top, r.Left, r.Bottom); XButtonBox.AddLine( r.Left + 18, r.Top, r.Right - r.Height, r.Top); XButtonBox.AddArc( r.Right - r.Height, r.Top, r.Height, r.Height, -90, 180); XButtonBox.AddLine( r.Right - r.Height, r.Bottom, r.Left, r.Bottom); break; case GradualTitleBarType.Rectangular: XButtonBox.AddLine( r.Left, r.Top, r.Left, r.Bottom); XButtonBox.AddLine( r.Left, r.Top, r.Right - r.Height, r.Top); XButtonBox.AddArc( r.Right - r.Height, r.Top, r.Height, r.Height, -90, 180); XButtonBox.AddLine( r.Right - r.Height, r.Bottom, r.Left, r.Bottom); break; } return(XButtonBox); }
public GraphicsPath DrawArrow(Graphics graphics, Pen inPen, Brush inBrush, Point point, Point point_2, double Size, bool Selected, LinkState state, bool cycling, int YOffset, bool Hovered) { GraphicsPath path = null; double hyp = Math.Sqrt(Math.Pow(point_2.Y - point.Y, 2) + Math.Pow(point.X - point_2.X, 2)); if (hyp > 0) { Point Handle1 = new Point((int)((4 * point_2.X + point.X) / 5.0), point.Y); Point Handle2 = new Point((int)((4 * point.X + point_2.X) / 5.0), point_2.Y); if (cycling) { int Y = point.Y; int Y_2 = point.Y + Math.Abs(point.Y - point_2.Y); if (point.Y > point_2.Y) { Y = point_2.Y; Y_2 = point_2.Y + Math.Abs(point.Y - point_2.Y); } Y += (int)(YOffset * Size); Y_2 += (int)(YOffset * Size); Handle1 = new Point((int)(point.X + 100 * Size), Y); Handle2 = new Point((int)(point_2.X - 100 * Size), Y_2); } if (Hovered) { path = new GraphicsPath(); path.AddBezier(point, Handle1, Handle2, point_2); graphics.DrawPath(Layout.HoverPen, path); } else if (Selected) { path = new GraphicsPath(); path.AddBezier(point, Handle1, Handle2, point_2); graphics.DrawPath(Layout.FatPen, path); } path = new GraphicsPath(); path.AddBezier(point, Handle1, Handle2, point_2); graphics.DrawPath(inPen, path); //if (Hovered == true && path != null) //{ // Console.WriteLine("je suis dans Draw 1"); // Pen pe = new Pen(Color.Blue, 2); // graphics.DrawPath(pe, path); //} //if (Hovered == false && path != null) //{ // Console.WriteLine("je suis dans Draw 2"); // Pen pe = new Pen(Color.Red, 1); // graphics.DrawPath(pe, path); //} //condition ? true : false //graphics.DrawBezier(inPen, point, Handle1, Handle2, point_2); if (Size > 0) { int arrowSize = (int)(12 * Size); //Start if (state.StartArrow != LinksArrows.None) { DrawLinkBound(graphics, state.StartArrow, inBrush, point, arrowSize, 1); } //End if (state.EndArrow != LinksArrows.None) { DrawLinkBound(graphics, state.EndArrow, inBrush, point_2, arrowSize, -1); } } } return(path); }
public GraphicsPath GetGraphicsPath() { if (gp == null) { gp = new GraphicsPath(); PointF initPoint = new PointF(0, 0); PointF lastPoint = new PointF(0, 0); SvgPathSegList segments = (SvgPathSegList)PathSegList; SvgPathSeg segment; int nElems = segments.NumberOfItems; for (int i = 0; i < nElems; i++) { segment = (SvgPathSeg)segments.GetItem(i); if (segment is SvgPathSegMoveto) { SvgPathSegMoveto seg = (SvgPathSegMoveto)segment; gp.StartFigure(); lastPoint = initPoint = seg.AbsXY; } else if (segment is SvgPathSegLineto) { SvgPathSegLineto seg = (SvgPathSegLineto)segment; PointF p = seg.AbsXY; gp.AddLine(lastPoint.X, lastPoint.Y, p.X, p.Y); lastPoint = p; } else if (segment is SvgPathSegCurveto) { SvgPathSegCurveto seg = (SvgPathSegCurveto)segment; PointF xy = seg.AbsXY; PointF x1y1 = seg.CubicX1Y1; PointF x2y2 = seg.CubicX2Y2; gp.AddBezier(lastPoint.X, lastPoint.Y, x1y1.X, x1y1.Y, x2y2.X, x2y2.Y, xy.X, xy.Y); lastPoint = xy; } else if (segment is SvgPathSegArc) { SvgPathSegArc seg = (SvgPathSegArc)segment; PointF p = seg.AbsXY; if (lastPoint.Equals(p)) { // If the endpoints (x, y) and (x0, y0) are identical, then this // is equivalent to omitting the elliptical arc segment entirely. } else if (seg.R1 == 0 || seg.R2 == 0) { // Ensure radii are valid gp.AddLine(lastPoint, p); } else { CalculatedArcValues calcValues = seg.GetCalculatedArcValues(); GraphicsPath gp2 = new GraphicsPath(); gp2.StartFigure(); gp2.AddArc( calcValues.Cx - calcValues.CorrRx, calcValues.Cy - calcValues.CorrRy, calcValues.CorrRx * 2, calcValues.CorrRy * 2, calcValues.AngleStart, calcValues.AngleExtent ); Matrix matrix = new Matrix(); matrix.Translate( -calcValues.Cx, -calcValues.Cy ); gp2.Transform(matrix); matrix = new Matrix(); matrix.Rotate(seg.Angle); gp2.Transform(matrix); matrix = new Matrix(); matrix.Translate(calcValues.Cx, calcValues.Cy); gp2.Transform(matrix); gp.AddPath(gp2, true); } lastPoint = p; } else if (segment is SvgPathSegClosePath) { gp.CloseFigure(); lastPoint = initPoint; } } string fillRule = GetPropertyValue("fill-rule"); if (fillRule == "evenodd") { gp.FillMode = FillMode.Alternate; } else { gp.FillMode = FillMode.Winding; } } return(gp); }
public void DrawPath(IEnumerable <PathOperation> ops, Pen pen = null, BaseBrush baseBrush = null) { using (var path = new GraphicsPath()) { var bb = new BoundingBoxBuilder(); var position = Point.Zero; foreach (var op in ops) { var start = op as StartFigure; if (start != null) { path.StartFigure(); continue; } var moveTo = op as MoveTo; if (moveTo != null) { path.StartFigure(); continue; } var lineTo = op as LineTo; if (lineTo != null) { var p = lineTo.Start; path.AddLine(Conversions.GetPointF(lineTo.Start), Conversions.GetPointF(lineTo.End)); position = p; bb.Add(p); continue; } var arcTo = op as ArcTo; if (arcTo != null) { var p = arcTo.Point; path.AddLine(Conversions.GetPointF(position), Conversions.GetPointF(p)); position = p; bb.Add(p); continue; } var curveTo = op as CurveTo; if (curveTo != null) { path.AddBezier(Conversions.GetPointF(curveTo.Start), Conversions.GetPointF(curveTo.FirstControlPoint), Conversions.GetPointF(curveTo.SecondControlPoint), Conversions.GetPointF(curveTo.End)); bb.Add(curveTo.Start); bb.Add(curveTo.FirstControlPoint); bb.Add(curveTo.SecondControlPoint); bb.Add(curveTo.End); continue; } var closePath = op as ClosePath; if (closePath != null) { path.CloseFigure(); continue; } throw new NotSupportedException("Path Op " + op); } var frame = bb.BoundingBox; if (baseBrush != null) { graphics.FillPath(baseBrush.GetBrush(frame), path); } if (pen != null) { var r = Conversions.GetRectangleF(frame); graphics.DrawPath(pen.GetPen(), path); } } }
/// <summary> /// Zeichnet das erzeugte Vektorbild auf dem PictureBox /// </summary> void Draw() { if (ListOfCurveArray == null) { return; } Bitmap neuImage = new Bitmap(this.tableLayoutPanel1.Width / 2, this.tableLayoutPanel1.Height / 2); vectorizedImage_pictBox.Image = neuImage; Graphics neueZeichnung = Graphics.FromImage(neuImage); GraphicsPath neuerPfad = new GraphicsPath(); for (int i = 0; i < ListOfCurveArray.Count; i++) { ArrayList CurveArray = (ArrayList)ListOfCurveArray[i]; GraphicsPath Contour = null; GraphicsPath Hole = null; GraphicsPath Current = null; for (int j = 0; j < CurveArray.Count; j++) { if (j == 0) { Contour = new GraphicsPath(); Current = Contour; } else { Hole = new GraphicsPath(); Current = Hole; } Potrace.Curve[] Curves = (Potrace.Curve[])CurveArray[j]; for (int k = 0; k < Curves.Length; k++) { if (Curves[k].Kind == Potrace.CurveKind.Bezier) { Current.AddBezier((float)Curves[k].A.X, (float)Curves[k].A.Y, (float)Curves[k].ControlPointA.X, (float)Curves[k].ControlPointA.Y, (float)Curves[k].ControlPointB.X, (float)Curves[k].ControlPointB.Y, (float)Curves[k].B.X, (float)Curves[k].B.Y); } else { Current.AddLine((float)Curves[k].A.X, (float)Curves[k].A.Y, (float)Curves[k].B.X, (float)Curves[k].B.Y); } } if (j > 0) { Contour.AddPath(Hole, false); } neuerPfad.AddPath(Contour, false); } } if (befuellen_CheckBox.Checked) { neueZeichnung.FillPath(Brushes.DarkCyan, neuerPfad); } if (tracing_checkBox.Checked) { neueZeichnung.DrawPath(Pens.Black, neuerPfad); } if (punkte_anzeigen_checkbox.Checked) { Punkte_anzeigen(); } }
protected GraphicsPath ParsePath() { GraphicsPath result = new GraphicsPath(FillMode.Alternate); PointF lastPoint = new PointF(); MatchCollection matches = PARSE_REGEX.Matches(Data); foreach (Match match in matches) { char cmd = match.Value[0]; PointF[] points; string pointsStr = match.Value.Substring(1).Trim(); if (pointsStr.Length > 0) { string[] txtpoints = pointsStr.Split(new char[] { ',', ' ' }); if (txtpoints.Length == 1) { points = new PointF[1]; points[0].X = (float)TypeConverter.Convert(txtpoints[0], typeof(float)); } else { int c = txtpoints.Length / 2; points = new PointF[c]; for (int i = 0; i < c; i++) { points[i].X = (float)TypeConverter.Convert(txtpoints[i * 2], typeof(float)); if (i + 1 < txtpoints.Length) { points[i].Y = (float)TypeConverter.Convert(txtpoints[i * 2 + 1], typeof(float)); } } } } else { points = new PointF[] {} }; switch (cmd) { case 'm': { //Relative origin PointF point = points[0]; lastPoint = new PointF(lastPoint.X + point.X, lastPoint.Y + point.Y); result.StartFigure(); } break; case 'M': { //Absolute origin lastPoint = points[0]; result.StartFigure(); } break; case 'L': //Absolute Line foreach (PointF t in points) { result.AddLine(lastPoint, t); lastPoint = t; } break; case 'l': //Relative Line for (int i = 0; i < points.Length; ++i) { points[i].X += lastPoint.X; points[i].Y += lastPoint.Y; result.AddLine(lastPoint, points[i]); lastPoint = points[i]; } break; case 'H': { //Horizontal line to absolute X PointF point1 = new PointF(points[0].X, lastPoint.Y); result.AddLine(lastPoint, point1); lastPoint = new PointF(point1.X, point1.Y); } break; case 'h': { //Horizontal line to relative X PointF point1 = new PointF(lastPoint.X + points[0].X, lastPoint.Y); result.AddLine(lastPoint, point1); lastPoint = new PointF(point1.X, point1.Y); } break; case 'V': { //Vertical line to absolute y PointF point1 = new PointF(lastPoint.X, points[0].X); result.AddLine(lastPoint, point1); lastPoint = new PointF(point1.X, point1.Y); } break; case 'v': { //Vertical line to relative y PointF point1 = new PointF(lastPoint.X, lastPoint.Y + points[0].X); result.AddLine(lastPoint, point1); lastPoint = new PointF(point1.X, point1.Y); } break; case 'C': //Quadratic Bezier curve command C21,17,17,21,13,21 for (int i = 0; i < points.Length; i += 3) { result.AddBezier(lastPoint, points[i], points[i + 1], points[i + 2]); lastPoint = points[i + 2]; } break; case 'c': //Quadratic Bezier curve command for (int i = 0; i < points.Length; i += 3) { points[i].X += lastPoint.X; points[i].Y += lastPoint.Y; result.AddBezier(lastPoint, points[i], points[i + 1], points[i + 2]); lastPoint = points[i + 2]; } break; case 'F': //Set fill mode command if (points[0].X == 0.0f) { //the EvenOdd fill rule //Rule that determines whether a point is in the fill region by drawing a ray //from that point to infinity in any direction and counting the number of path //segments within the given shape that the ray crosses. If this number is odd, //the point is inside; if even, the point is outside. result.FillMode = FillMode.Alternate; } else if (points[0].X == 1.0f) { //the Nonzero fill rule. //Rule that determines whether a point is in the fill region of the //path by drawing a ray from that point to infinity in any direction //and then examining the places where a segment of the shape crosses //the ray. Starting with a count of zero, add one each time a segment //crosses the ray from left to right and subtract one each time a path //segment crosses the ray from right to left. After counting the crossings, //if the result is zero then the point is outside the path. Otherwise, it is inside. result.FillMode = FillMode.Winding; } break; case 'z': result.CloseFigure(); break; } } return(result); } }
public void Render(Graphics g, Train train) { var style = new TrainStyle(train, attrs); if (!style.CalcedShow) { return; } var ardps = train.GetArrDepsUnsorted(); var dir = GetTrainDirection(train); using (var pen = new Pen((Color)style.CalcedColor, style.CalcedWidth) { DashPattern = ds.ParseDashstyle(style.CalcedLineStyle) }) using (var brush = new SolidBrush((Color)style.CalcedColor)) { List <PointF> points = new List <PointF>(); bool hadFirstArrival = false, hadLastDeparture = false, isFirst = true; var stas = dir ? Enumerable.Reverse(stations) : stations; int trainTravelsRouteCount = 0; foreach (var sta in stas) { if (!ardps.ContainsKey(sta)) { continue; } var ardp = ardps[sta]; trainTravelsRouteCount++; if (!ardp.HasMinOneTimeSet) { continue; } MaybeAddPoint(points, GetGutterPoint(true, dir, stationOffsets[sta], ardp.Arrival)); MaybeAddPoint(points, GetInternalPoint(stationOffsets[sta], ardp.Arrival, ardp.ArrivalTrack)); foreach (var shunt in ardp.ShuntMoves) { MaybeAddPoint(points, GetInternalPoint(stationOffsets[sta], shunt.Time, shunt.SourceTrack)); MaybeAddPoint(points, GetInternalPoint(stationOffsets[sta], shunt.Time, shunt.TargetTrack)); } MaybeAddPoint(points, GetInternalPoint(stationOffsets[sta], ardp.Departure, ardp.DepartureTrack)); MaybeAddPoint(points, GetGutterPoint(false, dir, stationOffsets[sta], ardp.Departure)); hadLastDeparture = ardp.Departure != default; if (isFirst) { hadFirstArrival = ardp.Arrival != default; } isFirst = false; } // Halbe Linien bei Abfahrten / Ankünften ohne Gegenstelle if (attrs.DrawNetworkTrains) { var hly = !dir ? 20 : -20; if (hadLastDeparture) { points.Add(points.Last() + new Size(50, hly)); } if (hadFirstArrival) { points.Insert(0, points.First() - new Size(50, hly)); } } else if (trainTravelsRouteCount <= 1) { return; // This train has only one station on this route and we don't draw network trains. } if (points.Count == 0) { return; // This train is not travelling on this route } // Verbindung zum Folgezug var transition = tt.GetTransition(train); if (transition != null && !hadLastDeparture && attrs.StationLines != StationLineStyle.None) { var lastStaOfFirst = GetSortedStations(train)?.LastOrDefault(); var firstStaOfNext = GetSortedStations(transition)?.FirstOrDefault(); if (lastStaOfFirst == firstStaOfNext) { var departure = transition.GetArrDep(firstStaOfNext).Departure; points.Add(new PointF(points.Last().X, GetTimeY(departure))); } } using (var p = new GraphicsPath()) { for (int i = 0; i < points.Count; i += 1) { if (points.Count <= i + 1) { continue; } var isStationLine = (int)points[i].X == (int)points[i + 1].X; if (isStationLine) { var preX = i > 0 ? points[i - 1].X : 0; var postX = i < points.Count - 2 ? points[i + 2].X : 0; var x = points[i].X; var isTransition = isStationLine && (points.Count == i + 2 || Math.Sign(preX - x) == Math.Sign(postX - x)); float bezierFactor = !isTransition ? ((preX < postX) ? -1 : 1) : // preX < postX --> TrainDirection.ti Math.Sign(preX - x); // Bei Transitions if (isTransition) { bezierFactor *= 0.5f; } var bezierOffset = new SizeF(bezierFactor * 14, (points[i + 1].Y - points[i].Y) / -4.0f); var bezierOffsetT = new SizeF(bezierOffset.Width, -bezierOffset.Height); switch (attrs.StationLines) { case StationLineStyle.None: p.MoveTo(points[i + 1]); break; case StationLineStyle.Normal: p.AddLine(points[i], points[i + 1]); break; case StationLineStyle.Cubic: var control2 = points[i + 1] + (!isTransition ? bezierOffset : (SizeF.Empty - bezierOffsetT)); p.AddBezier(points[i], points[i] - bezierOffset, control2, points[i + 1]); break; } } else { p.AddLine(points[i], points[i + 1]); // Normale Zuglinie } if (points[i].X == points[i + 1].X || points[i].Y == points[i + 1].Y) { continue; } // Zugnummern zeichnen var trainFont = (Font)attrs.TrainFont; var size = g.MeasureString(trainFont, train.TName); float[] ys = { points[i].Y, points[i + 1].Y }; float[] xs = { points[i].X, points[i + 1].X }; float ty = ys.Min() + (ys.Max() - ys.Min()) / 2 - (size.Height / 2); float tx = xs.Min() + (xs.Max() - xs.Min()) / 2; if (g.Clip.IsVisible(new PointF(tx, ty))) // translated drawing does not respect clip (Idk why) { float angle = CalcAngle(ys, xs, train); var matrix = g.Transform.Clone(); g.TranslateTransform(tx, ty); g.RotateTransform(-angle); g.DrawText(trainFont, brush, -(size.Width / 2), -(size.Height / 2), train.TName); g.Transform = matrix; } } g.DrawPath(pen, p); } } }
GraphicsPath CreatePath() { var path = new GraphicsPath(); var start = StartFigures; var close = CloseFigures; // connected segments path.MoveTo(10, 10); path.LineTo(20, 90); path.LineTo(10, 60); path.LineTo(90, 80); path.LineTo(60, 30); if (close && start) { path.CloseFigure(); } if (start) { path.StartFigure(); } path.AddArc(100, 0, 100, 50, 200, -160); if (close && start) { path.CloseFigure(); } if (start) { path.StartFigure(); } path.AddBezier(new PointF(200, 10), new PointF(285, 20), new PointF(210, 85), new PointF(300, 90)); if (close && start) { path.CloseFigure(); } if (start) { path.StartFigure(); } path.AddCurve(new PointF(310, 90), new PointF(390, 90), new PointF(390, 10), new PointF(310, 10)); if (close && start) { path.CloseFigure(); } if (start) { path.StartFigure(); } path.AddLine(410, 10, 410, 90); if (close && start) { path.CloseFigure(); } if (start) { path.StartFigure(); } path.AddLines(new PointF(420, 10), new PointF(420, 90)); if (close && start) { path.CloseFigure(); } if (start) { path.StartFigure(); } path.AddLines(new PointF(430, 10), new PointF(430, 90)); if (close) { path.CloseFigure(); } // separate segments if (start) { path.StartFigure(); } path.AddEllipse(100, 100, 100, 45); if (close) { path.CloseFigure(); } if (start) { path.StartFigure(); } path.AddRectangle(10, 110, 80, 80); if (close) { path.CloseFigure(); } // at the end, draw a line so we can potentially connect to parent path if (start) { path.StartFigure(); } path.AddLines(new PointF(440, 10), new PointF(440, 90)); if (close) { path.CloseFigure(); } return(path); }
BezierLayoutInfo GetLayoutInfo(BezierControlPoint controlPoint1, BezierControlPoint controlPoint2) { var layout = new BezierLayoutInfo(); layout.CP1 = controlPoint1; layout.CP2 = controlPoint2; Point cp1, cp2; Rectangle rect1 = From.Bounds; Rectangle rect2 = Target.Bounds; Point pts = PaintHelper.CenterPoint(rect1); Point ptd = PaintHelper.CenterPoint(rect2); rect1.Inflate(2, 2); rect2.Inflate(2, 2); if (StartCap != LineAnchor.None) { rect1.Inflate(Layouter.LineAnchorSize, Layouter.LineAnchorSize); } if (EndCap != LineAnchor.None) { rect2.Inflate(Layouter.LineAnchorSize, Layouter.LineAnchorSize); } if (pts == ptd) { cp1 = pts; cp2 = ptd; layout.Bounds = Rectangle.Empty; layout.Region = null; } else { cp1 = BezierHelper.GetControlPoint(pts, controlPoint1); cp2 = BezierHelper.GetControlPoint(ptd, controlPoint2); //Point[] controlPoints = GetControlPoints(pts, ptd); Shape sShape = Shape.GetShaper(From); pts = sShape.GetBorderPoint(rect1, cp1); Shape dShape = Shape.GetShaper(Target); ptd = sShape.GetBorderPoint(rect2, cp2); GraphicsPath gp = new GraphicsPath(); gp.AddBezier(pts, cp1, cp2, ptd); Pen penWiden = new Pen(Color.Black, LineWidth + 5); gp.Widen(penWiden); var rect = gp.GetBounds(); rect.Inflate(LineWidth, LineWidth); layout.Bounds = rect; layout.Region = new Region(gp); } Point ptCenter = BezierHelper.GetPoint(pts, cp1, cp2, ptd, 0.5f); Rectangle textBounds = layout.TextBounds; textBounds.X = ptCenter.X - layout.TextBounds.Width / 2; textBounds.Y = ptCenter.Y - layout.TextBounds.Height / 2; // cache layout info layout.StartBounds = rect1; layout.EndBounds = rect2; layout.StartPoint = pts; layout.EndPoint = ptd; layout.ControlPoint1 = cp1; layout.ControlPoint2 = cp2; layout.TextBounds = textBounds; return(layout); }
private void IssuePathCommand(StringReader reader, GraphicsPath path, string command) { List <float[]> ArgumentList = new List <float[]>(); int ExpectedArguments; //figure out the number of floats that should be in each element of ArgumentList #region Deduce Argument Counts switch (command) { case "ARC": ExpectedArguments = 6; break; case "BEZIER": ExpectedArguments = 8; break; case "CURVE": case "POLYGON": ExpectedArguments = 2; break; case "LINE": case "LINES": case "ELLIPSE": case "RECTANGLE": case "RECTANGLES": ExpectedArguments = 4; break; default: throw new NotSupportedException(command); } #endregion //read all the arguments to the command string Line; while ((Line = reader.ReadLine()) != null && Line != "") //keep reading while we haven't hit the end of the file or a blank line { if (IsCommentLine(Line)) { continue; } string[] Parts = Line.Split(','); AssertFormat(Parts.Length == ExpectedArguments, string.Format("Invalid number of arguments to {0}. Expected {1}; found {2}.", command, ExpectedArguments, Parts.Length)); //convert the string parts to floats float[] fParts = new float[ExpectedArguments]; for (int i = 0; i < ExpectedArguments; i++) { fParts[i] = float.Parse(Parts[i]); } ArgumentList.Add(fParts); } //issue the command #region Command Issuance switch (command) { case "CURVE": //curve command requires an array of all points path.StartFigure(); path.AddCurve(ArgumentList.ConvertAll(new Converter <float[], PointF>( delegate(float[] p) { return(new PointF(p[0], p[1])); })).ToArray()); break; case "POLYGON": //polygon command requires an array of all points path.StartFigure(); path.AddPolygon(ArgumentList.ConvertAll(new Converter <float[], PointF>( delegate(float[] p) { return(new PointF(p[0], p[1])); })).ToArray()); break; default: foreach (float[] a in ArgumentList) { path.StartFigure(); //process each line of coordinates in series switch (command) { case "ARC": path.AddArc(a[0], a[1], a[2], a[3], a[4], a[5]); break; case "BEZIER": case "BEZIERS": path.AddBezier(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]); break; case "LINE": case "LINES": path.AddLine(a[0], a[1], a[2], a[3]); break; case "ELLIPSE": path.AddEllipse(a[0], a[1], a[2], a[3]); break; case "RECTANGLE": case "RECTANGLES": path.AddRectangle(new RectangleF(a[0], a[1], a[2], a[3])); break; } } break; } #endregion }
public override void AddToPath(GraphicsPath graphicsPath) { if (Start == End) { return; } if (RadiusX == 0.0f && RadiusY == 0.0f) { graphicsPath.AddLine(Start, End); return; } var sinPhi = Math.Sin(Angle * RadiansPerDegree); var cosPhi = Math.Cos(Angle * RadiansPerDegree); var x1dash = cosPhi * (Start.X - End.X) / 2.0 + sinPhi * (Start.Y - End.Y) / 2.0; var y1dash = -sinPhi * (Start.X - End.X) / 2.0 + cosPhi * (Start.Y - End.Y) / 2.0; double root; var numerator = RadiusX * RadiusX * RadiusY * RadiusY - RadiusX * RadiusX * y1dash * y1dash - RadiusY * RadiusY * x1dash * x1dash; var rx = RadiusX; var ry = RadiusY; if (numerator < 0.0) { var s = (float)Math.Sqrt(1.0 - numerator / (RadiusX * RadiusX * RadiusY * RadiusY)); rx *= s; ry *= s; root = 0.0; } else { root = ((Size == SvgArcSize.Large && Sweep == SvgArcSweep.Positive) || (Size == SvgArcSize.Small && Sweep == SvgArcSweep.Negative) ? -1.0 : 1.0) * Math.Sqrt(numerator / (RadiusX * RadiusX * y1dash * y1dash + RadiusY * RadiusY * x1dash * x1dash)); } var cxdash = root * rx * y1dash / ry; var cydash = -root * ry * x1dash / rx; var cx = cosPhi * cxdash - sinPhi * cydash + (Start.X + End.X) / 2.0; var cy = sinPhi * cxdash + cosPhi * cydash + (Start.Y + End.Y) / 2.0; var theta1 = CalculateVectorAngle(1.0, 0.0, (x1dash - cxdash) / rx, (y1dash - cydash) / ry); var dtheta = CalculateVectorAngle((x1dash - cxdash) / rx, (y1dash - cydash) / ry, (-x1dash - cxdash) / rx, (-y1dash - cydash) / ry); if (Sweep == SvgArcSweep.Negative && dtheta > 0) { dtheta -= 2.0 * Math.PI; } else if (Sweep == SvgArcSweep.Positive && dtheta < 0) { dtheta += 2.0 * Math.PI; } var segments = (int)Math.Ceiling((double)Math.Abs(dtheta / (Math.PI / 2.0))); var delta = dtheta / segments; var t = 8.0 / 3.0 * Math.Sin(delta / 4.0) * Math.Sin(delta / 4.0) / Math.Sin(delta / 2.0); var startX = Start.X; var startY = Start.Y; for (var i = 0; i < segments; ++i) { var cosTheta1 = Math.Cos(theta1); var sinTheta1 = Math.Sin(theta1); var theta2 = theta1 + delta; var cosTheta2 = Math.Cos(theta2); var sinTheta2 = Math.Sin(theta2); var endpointX = cosPhi * rx * cosTheta2 - sinPhi * ry * sinTheta2 + cx; var endpointY = sinPhi * rx * cosTheta2 + cosPhi * ry * sinTheta2 + cy; var dx1 = t * (-cosPhi * rx * sinTheta1 - sinPhi * ry * cosTheta1); var dy1 = t * (-sinPhi * rx * sinTheta1 + cosPhi * ry * cosTheta1); var dxe = t * (cosPhi * rx * sinTheta2 + sinPhi * ry * cosTheta2); var dye = t * (sinPhi * rx * sinTheta2 - cosPhi * ry * cosTheta2); graphicsPath.AddBezier(startX, startY, (float)(startX + dx1), (float)(startY + dy1), (float)(endpointX + dxe), (float)(endpointY + dye), (float)endpointX, (float)endpointY); theta1 = theta2; startX = (float)endpointX; startY = (float)endpointY; } }
/// <summary> /// 创建图像 /// </summary> /// <param name="size"></param> /// <returns></returns> protected override Image CreateSurfaceImage(Size size) { if (colors.Count == 0) { throw new InvalidOperationException(string.Format(System.Globalization.CultureInfo.CurrentUICulture, "Resources.Errors.NoColorsForLineNoise")); } Bitmap bitmap = new Bitmap(size.Width, size.Height); Random rnd = new Random(); using (Graphics graphics = Graphics.FromImage(bitmap)) { graphics.FillRectangle(Brushes.White, graphics.VisibleClipBounds); using (GraphicsPath path = new GraphicsPath()) { int width = size.Width - Margin; int height = size.Height - Margin; int minPosition = Margin + 5; int n = 0; Point end = Point.Empty; bool hasDrawnPath = false; using (Pen pen1 = new Pen(colors[rnd.Next(0, colors.Count)], 1)) { using (Pen pen2 = new Pen(colors[rnd.Next(0, colors.Count)], 1.8f)) { path.AddBezier( new Point(rnd.Next(Margin, Math.Max(minPosition, width)), rnd.Next(Margin, Math.Max(minPosition, height))), new Point(rnd.Next(Margin, Math.Max(minPosition, width)), rnd.Next(Margin, Math.Max(minPosition, height))), new Point(rnd.Next(Margin, Math.Max(minPosition, width)), rnd.Next(Margin, Math.Max(minPosition, height))), end = new Point(rnd.Next(Margin, Math.Max(minPosition, width)), rnd.Next(Margin, Math.Max(minPosition, height)))); n = rnd.Next(0, 11); if (n < 6) { graphics.DrawPath((n > 2) ? pen1 : pen2, path); path.Reset(); hasDrawnPath = true; } for (int i = 0; i < rnd.Next(0, 4); i++) { Point start = end; end = new Point(rnd.Next(Margin, Math.Max(minPosition, width)), rnd.Next(Margin, Math.Max(minPosition, height))); path.AddLine(start, end); n = rnd.Next(0, 11); if (n < 6) { graphics.DrawPath((n > 2) ? pen1 : pen2, path); path.Reset(); hasDrawnPath = true; } } path.AddBezier(end, new Point(rnd.Next(Margin, Math.Max(minPosition, width)), rnd.Next(Margin, Math.Max(minPosition, height))), new Point(rnd.Next(Margin, Math.Max(minPosition, width)), rnd.Next(Margin, Math.Max(minPosition, height))), new Point(rnd.Next(Margin, Math.Max(minPosition, width)), rnd.Next(Margin, Math.Max(minPosition, height)))); n = rnd.Next(0, 11); if (!hasDrawnPath || n < 6) { graphics.DrawPath((n > 2) ? pen1 : pen2, path); } } } } } return(bitmap); }
public static GraphicsPath CreatePath(SvgPathElement element) { GraphicsPath gp = new GraphicsPath(); SvgPointF initPoint = new SvgPointF(0, 0); SvgPointF lastPoint = new SvgPointF(0, 0); ISvgPathSeg segment = null; SvgPathSegMoveto pathMoveTo = null; SvgPathSegLineto pathLineTo = null; SvgPathSegCurveto pathCurveTo = null; SvgPathSegArc pathArc = null; ISvgPathSegList segments = element.PathSegList; int nElems = segments.NumberOfItems; for (int i = 0; i < nElems; i++) { segment = segments.GetItem(i); if (DynamicCast.Cast(segment, out pathMoveTo)) { //SvgPathSegMoveto seg = (SvgPathSegMoveto)segment; gp.StartFigure(); lastPoint = initPoint = pathMoveTo.AbsXY; } else if (DynamicCast.Cast(segment, out pathLineTo)) { //SvgPathSegLineto seg = (SvgPathSegLineto)segment; SvgPointF p = pathLineTo.AbsXY; gp.AddLine(lastPoint.X, lastPoint.Y, p.X, p.Y); lastPoint = p; } else if (DynamicCast.Cast(segment, out pathCurveTo)) { // SvgPathSegCurveto seg = (SvgPathSegCurveto)segment; SvgPointF xy = pathCurveTo.AbsXY; SvgPointF x1y1 = pathCurveTo.CubicX1Y1; SvgPointF x2y2 = pathCurveTo.CubicX2Y2; gp.AddBezier(lastPoint.X, lastPoint.Y, x1y1.X, x1y1.Y, x2y2.X, x2y2.Y, xy.X, xy.Y); lastPoint = xy; } else if (DynamicCast.Cast(segment, out pathArc)) { //SvgPathSegArc seg = (SvgPathSegArc)segment; SvgPointF p = pathArc.AbsXY; if (lastPoint.Equals(p)) { // If the endpoints (x, y) and (x0, y0) are identical, then this // is equivalent to omitting the elliptical arc segment entirely. } else if (pathArc.R1 == 0 || pathArc.R2 == 0) { // Ensure radii are valid gp.AddLine(lastPoint.X, lastPoint.Y, p.X, p.Y); } else { CalculatedArcValues calcValues = pathArc.GetCalculatedArcValues(); GraphicsPath gp2 = new GraphicsPath(); gp2.StartFigure(); gp2.AddArc((float)(calcValues.Cx - calcValues.CorrRx), (float)(calcValues.Cy - calcValues.CorrRy), (float)calcValues.CorrRx * 2, (float)calcValues.CorrRy * 2, (float)calcValues.AngleStart, (float)calcValues.AngleExtent); Matrix matrix = new Matrix(); matrix.Translate(-(float)calcValues.Cx, -(float)calcValues.Cy); gp2.Transform(matrix); matrix = new Matrix(); matrix.Rotate((float)pathArc.Angle); gp2.Transform(matrix); matrix = new Matrix(); matrix.Translate((float)calcValues.Cx, (float)calcValues.Cy); gp2.Transform(matrix); gp.AddPath(gp2, true); } lastPoint = p; } else if (segment is SvgPathSegClosePath) { gp.CloseFigure(); lastPoint = initPoint; } } string fillRule = element.GetPropertyValue("fill-rule"); if (fillRule == "evenodd") { gp.FillMode = FillMode.Alternate; } else { gp.FillMode = FillMode.Winding; } return(gp); }
static void ScissorsTest(Graphics graphics) { var t = new { CenterX = 0f, CenterY = 0f, OffsetX = 200f, OffsetY = 50f, RotateAngle = 0f, SkewAngleY = 10f, SkewAngleX = 10f, ScaleX = 16f, ScaleY = 16f }; var path = new GraphicsPath(); path.FillMode = FillMode.Winding; // draw some scissors path.AddBezier(new PointF(9.64f, 7.64f), new PointF(9.87f, 7.14f), new PointF(10f, 6.59f), new PointF(10f, 6f)); path.AddBezier(new PointF(10f, 6f), new PointF(10f, 3.79f), new PointF(8.21f, 2f), new PointF(6f, 2f)); path.AddBezier(new PointF(6f, 2f), new PointF(3.79f, 2f), new PointF(2f, 3.79f), new PointF(2f, 6f)); path.AddBezier(new PointF(2f, 6f), new PointF(2f, 8.21f), new PointF(3.79f, 10f), new PointF(6f, 10f)); path.AddBezier(new PointF(6f, 10f), new PointF(6.59f, 10f), new PointF(7.14f, 9.87f), new PointF(7.64f, 9.64f)); path.AddLine(7.64f, 9.64f, 10f, 12f); path.AddLine(10f, 12f, 7.64f, 14.36f); path.AddBezier(new PointF(7.64f, 14.36f), new PointF(7.14f, 14.13f), new PointF(6.59f, 14f), new PointF(6f, 14f)); path.AddBezier(new PointF(6f, 14f), new PointF(3.79f, 14f), new PointF(2f, 15.79f), new PointF(2f, 18f)); path.AddBezier(new PointF(2f, 18f), new PointF(2f, 20.21f), new PointF(3.79f, 22f), new PointF(6f, 22f)); path.AddBezier(new PointF(6f, 22f), new PointF(8.21f, 22f), new PointF(10f, 20.21f), new PointF(10f, 18f)); path.AddBezier(new PointF(10f, 18f), new PointF(10f, 17.41f), new PointF(9.87f, 16.86f), new PointF(9.64f, 16.36f)); path.AddLine(9.64f, 16.36f, 12f, 14f); path.AddLine(12f, 14f, 19f, 21f); path.AddLine(19f, 21f, 22f, 21f); path.AddLine(22f, 21f, 22f, 20f); path.AddLine(22f, 20f, 9.64f, 7.64f); path.CloseFigure(); path.AddBezier(new PointF(6f, 8f), new PointF(4.9f, 8f), new PointF(4f, 7.11f), new PointF(4f, 6f)); path.AddBezier(new PointF(4f, 6f), new PointF(4f, 4.89f), new PointF(4.9f, 4f), new PointF(6f, 4f)); path.AddBezier(new PointF(6f, 4f), new PointF(7.1f, 4f), new PointF(8f, 4.89f), new PointF(8f, 6f)); path.AddBezier(new PointF(8f, 6f), new PointF(8f, 7.11f), new PointF(7.1f, 8f), new PointF(6f, 8f)); path.CloseFigure(); path.AddBezier(new PointF(6f, 20f), new PointF(4.9f, 20f), new PointF(4f, 19.11f), new PointF(4f, 18f)); path.AddBezier(new PointF(4f, 18f), new PointF(4f, 16.89f), new PointF(4.9f, 16f), new PointF(6f, 16f)); path.AddBezier(new PointF(6f, 16f), new PointF(7.1f, 16f), new PointF(8f, 16.89f), new PointF(8f, 18f)); path.AddBezier(new PointF(8f, 18f), new PointF(8f, 19.11f), new PointF(7.1f, 20f), new PointF(6f, 20f)); path.CloseFigure(); path.AddBezier(new PointF(12f, 12.5f), new PointF(11.72f, 12.5f), new PointF(11.5f, 12.28f), new PointF(11.5f, 12f)); path.AddBezier(new PointF(11.5f, 12f), new PointF(11.5f, 11.72f), new PointF(11.72f, 11.5f), new PointF(12f, 11.5f)); path.AddBezier(new PointF(12f, 11.5f), new PointF(12.28f, 11.5f), new PointF(12.5f, 11.72f), new PointF(12.5f, 12f)); path.AddBezier(new PointF(12.5f, 12f), new PointF(12.5f, 12.28f), new PointF(12.28f, 12.5f), new PointF(12f, 12.5f)); path.CloseFigure(); path.AddLine(19f, 3f, 13f, 9f); path.AddLine(13f, 9f, 15f, 11f); path.AddLine(15f, 11f, 22f, 4f); path.AddLine(22f, 4f, 22f, 3f); path.CloseFigure(); path.AddBezier(new PointF(9.64f, 7.64f), new PointF(9.87f, 7.14f), new PointF(10f, 6.59f), new PointF(10f, 6f)); path.AddBezier(new PointF(10f, 6f), new PointF(10f, 3.79f), new PointF(8.21f, 2f), new PointF(6f, 2f)); path.AddBezier(new PointF(6f, 2f), new PointF(3.79f, 2f), new PointF(2f, 3.79f), new PointF(2f, 6f)); path.AddBezier(new PointF(2f, 6f), new PointF(2f, 8.21f), new PointF(3.79f, 10f), new PointF(6f, 10f)); path.AddBezier(new PointF(6f, 10f), new PointF(6.59f, 10f), new PointF(7.14f, 9.87f), new PointF(7.64f, 9.64f)); path.AddLine(7.64f, 9.64f, 10f, 12f); path.AddLine(10f, 12f, 7.64f, 14.36f); path.AddBezier(new PointF(7.64f, 14.36f), new PointF(7.14f, 14.13f), new PointF(6.59f, 14f), new PointF(6f, 14f)); path.AddBezier(new PointF(6f, 14f), new PointF(3.79f, 14f), new PointF(2f, 15.79f), new PointF(2f, 18f)); path.AddBezier(new PointF(2f, 18f), new PointF(2f, 20.21f), new PointF(3.79f, 22f), new PointF(6f, 22f)); path.AddBezier(new PointF(6f, 22f), new PointF(8.21f, 22f), new PointF(10f, 20.21f), new PointF(10f, 18f)); path.AddBezier(new PointF(10f, 18f), new PointF(10f, 17.41f), new PointF(9.87f, 16.86f), new PointF(9.64f, 16.36f)); path.AddLine(9.64f, 16.36f, 12f, 14f); path.AddLine(12f, 14f, 19f, 21f); path.AddLine(19f, 21f, 22f, 21f); path.AddLine(22f, 21f, 22f, 20f); path.AddLine(22f, 20f, 9.64f, 7.64f); path.CloseFigure(); path.AddBezier(new PointF(6f, 8f), new PointF(4.9f, 8f), new PointF(4f, 7.11f), new PointF(4f, 6f)); path.AddBezier(new PointF(4f, 6f), new PointF(4f, 4.89f), new PointF(4.9f, 4f), new PointF(6f, 4f)); path.AddBezier(new PointF(6f, 4f), new PointF(7.1f, 4f), new PointF(8f, 4.89f), new PointF(8f, 6f)); path.AddBezier(new PointF(8f, 6f), new PointF(8f, 7.11f), new PointF(7.1f, 8f), new PointF(6f, 8f)); path.CloseFigure(); path.AddBezier(new PointF(6f, 20f), new PointF(4.9f, 20f), new PointF(4f, 19.11f), new PointF(4f, 18f)); path.AddBezier(new PointF(4f, 18f), new PointF(4f, 16.89f), new PointF(4.9f, 16f), new PointF(6f, 16f)); path.AddBezier(new PointF(6f, 16f), new PointF(7.1f, 16f), new PointF(8f, 16.89f), new PointF(8f, 18f)); path.AddBezier(new PointF(8f, 18f), new PointF(8f, 19.11f), new PointF(7.1f, 20f), new PointF(6f, 20f)); path.CloseFigure(); path.AddBezier(new PointF(12f, 12.5f), new PointF(11.72f, 12.5f), new PointF(11.5f, 12.28f), new PointF(11.5f, 12f)); path.AddBezier(new PointF(11.5f, 12f), new PointF(11.5f, 11.72f), new PointF(11.72f, 11.5f), new PointF(12f, 11.5f)); path.AddBezier(new PointF(12f, 11.5f), new PointF(12.28f, 11.5f), new PointF(12.5f, 11.72f), new PointF(12.5f, 12f)); path.AddBezier(new PointF(12.5f, 12f), new PointF(12.5f, 12.28f), new PointF(12.28f, 12.5f), new PointF(12f, 12.5f)); path.CloseFigure(); path.AddLine(19f, 3f, 13f, 9f); path.AddLine(13f, 9f, 15f, 11f); path.AddLine(15f, 11f, 22f, 4f); path.AddLine(22f, 4f, 22f, 3f); path.CloseFigure(); var m = Matrix.Create(); var c = new PointF((float)t.CenterX, (float)t.CenterY); // translate m.Translate((float)t.OffsetX, (float)t.OffsetY); // rotate m.RotateAt((float)t.RotateAngle, c); // skew m.Translate(-c.X, -c.Y); m.Prepend(Matrix.Create(1, (float)Math.Tan(Math.PI * t.SkewAngleY / 180.0), (float)Math.Tan(Math.PI * t.SkewAngleX / 180.0), 1, 0, 0)); m.Translate(c.X, c.Y); // scale m.ScaleAt((float)t.ScaleX, (float)t.ScaleY, (float)t.CenterX, (float)t.CenterY); graphics.SaveTransform(); graphics.MultiplyTransform(m); var brush = new SolidBrush(Colors.Black); var pen = new Pen(Colors.Red, 0.5f); graphics.FillPath(brush, path); graphics.DrawPath(pen, path); brush.Dispose(); pen.Dispose(); graphics.RestoreTransform(); }
public void QuadraticCurveTo(float cpx, float cpy, float x, float y) { _currentPath.AddBezier(_currentX, _currentY, cpx, cpy, cpx, cpy, x, y); _currentX = x; _currentY = y; }
/// <summary> /// /// </summary> /// <param name="pg"></param> /// <param name="dx"></param> /// <param name="dy"></param> /// <param name="scale"></param> /// <returns></returns> public static GraphicsPath ToGraphicsPath(this XPathGeometry pg, double dx, double dy, Func <double, float> scale) { var gp = new GraphicsPath(); gp.FillMode = pg.FillRule == Core2D.XFillRule.EvenOdd ? FillMode.Alternate : FillMode.Winding; foreach (var pf in pg.Figures) { var startPoint = pf.StartPoint; foreach (var segment in pf.Segments) { if (segment is Core2D.XArcSegment) { throw new NotSupportedException("Not supported segment type: " + segment.GetType()); //var arcSegment = segment as Core2D.XArcSegment; // TODO: Convert WPF/SVG elliptical arc segment format to GDI+ bezier curves. //startPoint = arcSegment.Point; } else if (segment is Core2D.XBezierSegment) { var bezierSegment = segment as Core2D.XBezierSegment; gp.AddBezier( new PointF( scale(startPoint.X), scale(startPoint.Y)), new PointF( scale(bezierSegment.Point1.X), scale(bezierSegment.Point1.Y)), new PointF( scale(bezierSegment.Point2.X), scale(bezierSegment.Point2.Y)), new PointF( scale(bezierSegment.Point3.X), scale(bezierSegment.Point3.Y))); startPoint = bezierSegment.Point3; } else if (segment is Core2D.XLineSegment) { var lineSegment = segment as Core2D.XLineSegment; gp.AddLine( scale(startPoint.X), scale(startPoint.Y), scale(lineSegment.Point.X), scale(lineSegment.Point.Y)); startPoint = lineSegment.Point; } else if (segment is Core2D.XPolyBezierSegment) { var polyBezierSegment = segment as Core2D.XPolyBezierSegment; if (polyBezierSegment.Points.Count >= 3) { gp.AddBezier( new PointF( scale(startPoint.X), scale(startPoint.Y)), new PointF( scale(polyBezierSegment.Points[0].X), scale(polyBezierSegment.Points[0].Y)), new PointF( scale(polyBezierSegment.Points[1].X), scale(polyBezierSegment.Points[1].Y)), new PointF( scale(polyBezierSegment.Points[2].X), scale(polyBezierSegment.Points[2].Y))); } if (polyBezierSegment.Points.Count > 3 && polyBezierSegment.Points.Count % 3 == 0) { for (int i = 3; i < polyBezierSegment.Points.Count; i += 3) { gp.AddBezier( new PointF( scale(polyBezierSegment.Points[i - 1].X), scale(polyBezierSegment.Points[i - 1].Y)), new PointF( scale(polyBezierSegment.Points[i].X), scale(polyBezierSegment.Points[i].Y)), new PointF( scale(polyBezierSegment.Points[i + 1].X), scale(polyBezierSegment.Points[i + 1].Y)), new PointF( scale(polyBezierSegment.Points[i + 2].X), scale(polyBezierSegment.Points[i + 2].Y))); } } startPoint = polyBezierSegment.Points.Last(); } else if (segment is Core2D.XPolyLineSegment) { var polyLineSegment = segment as Core2D.XPolyLineSegment; if (polyLineSegment.Points.Count >= 1) { gp.AddLine( scale(startPoint.X), scale(startPoint.Y), scale(polyLineSegment.Points[0].X), scale(polyLineSegment.Points[0].Y)); } if (polyLineSegment.Points.Count > 1) { for (int i = 1; i < polyLineSegment.Points.Count; i++) { gp.AddLine( scale(polyLineSegment.Points[i - 1].X), scale(polyLineSegment.Points[i - 1].Y), scale(polyLineSegment.Points[i].X), scale(polyLineSegment.Points[i].Y)); } } startPoint = polyLineSegment.Points.Last(); } else if (segment is Core2D.XPolyQuadraticBezierSegment) { var polyQuadraticSegment = segment as Core2D.XPolyQuadraticBezierSegment; if (polyQuadraticSegment.Points.Count >= 2) { var p1 = startPoint; var p2 = polyQuadraticSegment.Points[0]; var p3 = polyQuadraticSegment.Points[1]; double x1 = p1.X; double y1 = p1.Y; double x2 = p1.X + (2.0 * (p2.X - p1.X)) / 3.0; double y2 = p1.Y + (2.0 * (p2.Y - p1.Y)) / 3.0; double x3 = x2 + (p3.X - p1.X) / 3.0; double y3 = y2 + (p3.Y - p1.Y) / 3.0; double x4 = p3.X; double y4 = p3.Y; gp.AddBezier( new PointF( scale(x1 + dx), scale(y1 + dy)), new PointF( scale(x2 + dx), scale(y2 + dy)), new PointF( scale(x3 + dx), scale(y3 + dy)), new PointF( scale(x4 + dx), scale(y4 + dy))); } if (polyQuadraticSegment.Points.Count > 2 && polyQuadraticSegment.Points.Count % 2 == 0) { for (int i = 3; i < polyQuadraticSegment.Points.Count; i += 3) { var p1 = polyQuadraticSegment.Points[i - 1]; var p2 = polyQuadraticSegment.Points[i]; var p3 = polyQuadraticSegment.Points[i + 1]; double x1 = p1.X; double y1 = p1.Y; double x2 = p1.X + (2.0 * (p2.X - p1.X)) / 3.0; double y2 = p1.Y + (2.0 * (p2.Y - p1.Y)) / 3.0; double x3 = x2 + (p3.X - p1.X) / 3.0; double y3 = y2 + (p3.Y - p1.Y) / 3.0; double x4 = p3.X; double y4 = p3.Y; gp.AddBezier( new PointF( scale(x1 + dx), scale(y1 + dy)), new PointF( scale(x2 + dx), scale(y2 + dy)), new PointF( scale(x3 + dx), scale(y3 + dy)), new PointF( scale(x4 + dx), scale(y4 + dy))); } } startPoint = polyQuadraticSegment.Points.Last(); } else if (segment is Core2D.XQuadraticBezierSegment) { var qbezierSegment = segment as Core2D.XQuadraticBezierSegment; var p1 = startPoint; var p2 = qbezierSegment.Point1; var p3 = qbezierSegment.Point2; double x1 = p1.X; double y1 = p1.Y; double x2 = p1.X + (2.0 * (p2.X - p1.X)) / 3.0; double y2 = p1.Y + (2.0 * (p2.Y - p1.Y)) / 3.0; double x3 = x2 + (p3.X - p1.X) / 3.0; double y3 = y2 + (p3.Y - p1.Y) / 3.0; double x4 = p3.X; double y4 = p3.Y; gp.AddBezier( new PointF( scale(x1 + dx), scale(y1 + dy)), new PointF( scale(x2 + dx), scale(y2 + dy)), new PointF( scale(x3 + dx), scale(y3 + dy)), new PointF( scale(x4 + dx), scale(y4 + dy))); startPoint = qbezierSegment.Point2; } else { throw new NotSupportedException("Not supported segment type: " + segment.GetType()); } } if (pf.IsClosed) { gp.CloseFigure(); } else { gp.StartFigure(); } } return(gp); }
/// <summary> /// Draws a round rectangle. /// </summary> /// <param name="clientRectangle">The region where to draw.</param> /// <param name="g">The GDI used to draw the rectangle.</param> private void DrawRoundRectangle(Rectangle clientRectangle, Graphics g) { double percentX = clientRectangle.Width * _roundPercent; double percentY = clientRectangle.Height * _roundPercent; double minPercent = Math.Min(percentX, percentY); double halfPercentX = percentX / 2; double halfPercentY = percentY / 2; double minHalfPercent = Math.Min(halfPercentX, halfPercentY); PointF pointUp1 = new PointF((float)(clientRectangle.X + minPercent), clientRectangle.Y); PointF pointUp2 = new PointF((float)(clientRectangle.X + clientRectangle.Width - minPercent), clientRectangle.Y); PointF pointDown1 = new PointF((float)(clientRectangle.X + clientRectangle.Width - minPercent), clientRectangle.Y + clientRectangle.Height); PointF pointDown2 = new PointF((float)(clientRectangle.X + minPercent), clientRectangle.Y + clientRectangle.Height); PointF pointLeft1 = new PointF(clientRectangle.X, (float)(clientRectangle.Y + clientRectangle.Height - minPercent)); PointF pointLeft2 = new PointF(clientRectangle.X, (float)(clientRectangle.Y + minPercent)); PointF pointRight1 = new PointF(clientRectangle.X + clientRectangle.Width, (float)(clientRectangle.Y + minPercent)); PointF pointRight2 = new PointF(clientRectangle.X + clientRectangle.Width, (float)(clientRectangle.Y + clientRectangle.Height - minPercent)); PointF pointCornerA1 = new PointF(clientRectangle.X, (float)(clientRectangle.Y + minHalfPercent)); PointF pointCornerA2 = new PointF((float)(clientRectangle.X + minHalfPercent), clientRectangle.Y); PointF pointCornerB1 = new PointF((float)(clientRectangle.X + clientRectangle.Width - minHalfPercent), clientRectangle.Y); PointF pointCornerB2 = new PointF(clientRectangle.X + clientRectangle.Width, (float)(clientRectangle.Y + minHalfPercent)); PointF pointCornerC1 = new PointF(clientRectangle.X + clientRectangle.Width, (float)(clientRectangle.Y + clientRectangle.Height - minHalfPercent)); PointF pointCornerC2 = new PointF((float)(clientRectangle.X + clientRectangle.Width - minHalfPercent), clientRectangle.Y + clientRectangle.Height); PointF pointCornerD1 = new PointF((float)(clientRectangle.X + minHalfPercent), clientRectangle.Y + clientRectangle.Height); PointF pointCornerD2 = new PointF(clientRectangle.X, (float)(clientRectangle.Y + clientRectangle.Height - minHalfPercent)); if ((_backStyle != BackStyleEnum.Transparent) || (_fillStyle != FillStyleEnum.Transparent)) { using (GraphicsPath graphicsPath = new GraphicsPath()) { graphicsPath.AddLine(pointUp1, pointUp2); graphicsPath.AddBezier(pointUp2, pointCornerB1, pointCornerB2, pointRight1); graphicsPath.AddLine(pointRight1, pointRight2); graphicsPath.AddBezier(pointRight2, pointCornerC1, pointCornerC2, pointDown1); graphicsPath.AddLine(pointDown1, pointDown2); graphicsPath.AddBezier(pointDown2, pointCornerD1, pointCornerD2, pointLeft1); graphicsPath.AddLine(pointLeft1, pointLeft2); graphicsPath.AddBezier(pointLeft2, pointCornerA1, pointCornerA2, pointUp1); using (Region region = new Region(graphicsPath)) { g.FillRegion(_shapeBrush, region); } } } g.DrawLine(shapePen, pointUp1, pointUp2); g.DrawLine(shapePen, pointDown1, pointDown2); g.DrawLine(shapePen, pointLeft1, pointLeft2); g.DrawLine(shapePen, pointRight1, pointRight2); g.DrawBezier(shapePen, pointLeft2, pointCornerA1, pointCornerA2, pointUp1); g.DrawBezier(shapePen, pointUp2, pointCornerB1, pointCornerB2, pointRight1); g.DrawBezier(shapePen, pointRight2, pointCornerC1, pointCornerC2, pointDown1); g.DrawBezier(shapePen, pointDown2, pointCornerD1, pointCornerD2, pointLeft1); }
void Genrate_btn_Click(object sender, EventArgs e) { Bitmap img = new Bitmap(CaptachaBox.Width, CaptachaBox.Height); Graphics graphics = Graphics.FromImage(img); // creates a white background graphics.FillRectangle(Brushes.White, 0, 0, img.Width, img.Height); Random random = new Random(DateTime.Now.Millisecond); int x1 = random.Next(10, 20); int y1 = random.Next(15, 25); int x2 = random.Next(50, 100); int y2 = (random.Next(0, 2) == 0 ? (-1) : 1) * random.Next(30, 50); int x3 = random.Next(100, 130); int y3 = (random.Next(0, 2) == 0 ? (-1) : 1) * random.Next(30, 50); int x4 = random.Next(240, 250); int y4 = random.Next(15, 25); // (random.Next(0,2) == 0 ? (-1) : 1) * GraphicsPath path = new GraphicsPath(); path.AddBezier ( new Point(x1, y1), new Point(x2, y2), new Point(x3, y3), new Point(x4, y4) ); CurrentWordIndex = random.Next(0, words.Length); DrawTextOnPath ( graphics, new SolidBrush(Color.Black), new Font ( fonts[random.Next(0, fonts.Length)], random.Next(25, 40) ), words[CurrentWordIndex], path, false ); graphics.DrawBezier ( new Pen(Color.Black, random.Next(1, 4)), new Point(x1, y1 + 30), new Point(x2, y2 + 30), new Point(x3, y3 + 30), new Point(x4, y4 + 30) ); CaptachaBox.Image = img; Controls.Add(AnswerBox); Controls.Add(Submit_btn); }
public static SharpDX.Direct2D1.Bitmap RedrawRestNoteBitmapCache(RenderTarget renderTargetSource, Color color, DurationTypes durationType) { ImagingFactory factory = new ImagingFactory(); SharpDX.Direct2D1.Factory factory2 = new SharpDX.Direct2D1.Factory(); SharpDX.DirectWrite.Factory factory3 = new SharpDX.DirectWrite.Factory(); SharpDX.WIC.Bitmap wicBitmap = new SharpDX.WIC.Bitmap(factory, 0x20, 0x30, SharpDX.WIC.PixelFormat.Format32bppPBGRA, BitmapCreateCacheOption.CacheOnDemand); RenderTargetProperties renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Unknown), 0f, 0f, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); WicRenderTarget renderTarget = new WicRenderTarget(factory2, wicBitmap, renderTargetProperties); try { PointF[] pathPoints; renderTarget.BeginDraw(); renderTarget.Clear(new RawColor4?(Color.Transparent.ToRawColor4(1f))); RawVector2 pos = new RawVector2(18f, 44f); using (GraphicsPath path = new GraphicsPath()) { DurationTypes types = durationType; if (types <= DurationTypes.Eighth) { switch (types) { case DurationTypes.The32nd: pos = new RawVector2(pos.X, (pos.Y - (8f * (((float)McMeasure.StaveSpacing) / 2f))) + 3f); path.AddLine(pos.X, pos.Y, pos.X + 1.6f, pos.Y); path.AddLine(pos.X + 1.6f, pos.Y, (pos.X - 8f) + 2.4f, pos.Y + 41f); path.AddLine((float)((pos.X - 8f) + 2.4f), (float)(pos.Y + 41f), (float)((pos.X - 8f) - 0.8f), (float)(pos.Y + 41f)); path.AddLine((pos.X - 8f) - 0.8f, pos.Y + 41f, pos.X, pos.Y); DrawRestDotPart(renderTarget, pos, color); DrawRestDotPart(renderTarget, new RawVector2(pos.X - 2.2f, pos.Y + 11f), color); DrawRestDotPart(renderTarget, new RawVector2(pos.X - 4.4f, pos.Y + 22f), color); break; case DurationTypes.The16th: pos = new RawVector2(pos.X, (pos.Y - (6f * (((float)McMeasure.StaveSpacing) / 2f))) + 3f); path.AddLine(pos.X, pos.Y, pos.X + 1.6f, pos.Y); path.AddLine(pos.X + 1.6f, pos.Y, (pos.X - 8f) + 2.4f, pos.Y + 30f); path.AddLine((float)((pos.X - 8f) + 2.4f), (float)(pos.Y + 30f), (float)((pos.X - 8f) - 0.8f), (float)(pos.Y + 30f)); path.AddLine((pos.X - 8f) - 0.8f, pos.Y + 30f, pos.X, pos.Y); DrawRestDotPart(renderTarget, pos, color); DrawRestDotPart(renderTarget, new RawVector2(pos.X - 2.4f, pos.Y + 11f), color); break; case DurationTypes.Eighth: goto Label_06F6; } } else { switch (types) { case DurationTypes.Quarter: pos = new RawVector2(pos.X - 11f, pos.Y - (7f * (((float)McMeasure.StaveSpacing) / 2f))); path.AddLine(pos.X, pos.Y, pos.X + 7f, pos.Y + 9.5f); path.AddBezier((float)(pos.X + 7f), (float)(pos.Y + 9.5f), (float)(pos.X + 3f), (float)(pos.Y + 16f), (float)(pos.X + 3f), (float)(pos.Y + 16f), (float)(pos.X + 8f), (float)(pos.Y + 24f)); path.AddBezier((float)(pos.X + 8f), (float)(pos.Y + 24f), (float)(pos.X - 0f), (float)(pos.Y + 21f), (float)(pos.X - 1f), (float)(pos.Y + 26f), (float)(pos.X + 4f), (float)(pos.Y + 34f)); path.AddBezier((float)(pos.X + 4f), (float)(pos.Y + 34f), (float)(pos.X - 8f), (float)(pos.Y + 28f), (float)(pos.X - 4f), (float)(pos.Y + 17f), (float)(pos.X + 4.5f), (float)(pos.Y + 21f)); path.AddLine((float)(pos.X + 4.5f), (float)(pos.Y + 21f), (float)(pos.X - 1f), (float)(pos.Y + 14f)); path.AddBezier((float)(pos.X - 1f), (float)(pos.Y + 14f), (float)(pos.X + 3.5f), (float)(pos.Y + 11f), (float)(pos.X + 4f), (float)(pos.Y + 7f), (float)(pos.X - 1f), (float)(pos.Y + 2f)); goto Label_0AD7; case DurationTypes.Half: pos = new RawVector2(pos.X - 8f, pos.Y - (4f * (((float)McMeasure.StaveSpacing) / 2f))); path.AddLine(pos.X - 7f, pos.Y, pos.X + 7f, pos.Y); path.AddLine(pos.X + 7f, pos.Y, pos.X + 7f, pos.Y - 5f); path.AddLine((float)(pos.X + 7f), (float)(pos.Y - 5f), (float)(pos.X - 7f), (float)(pos.Y - 5f)); path.AddLine(pos.X - 7f, pos.Y - 5f, pos.X - 7f, pos.Y); path.AddLine(pos.X - 7f, pos.Y, pos.X - 10f, pos.Y); path.AddLine(pos.X - 10f, pos.Y, pos.X - 10f, pos.Y + 1f); path.AddLine((float)(pos.X - 10f), (float)(pos.Y + 1f), (float)(pos.X + 10f), (float)(pos.Y + 1f)); path.AddLine(pos.X + 10f, pos.Y + 1f, pos.X + 10f, pos.Y); goto Label_0AD7; } if (types == DurationTypes.Whole) { pos = new RawVector2(pos.X - 8f, pos.Y - (6f * (((float)McMeasure.StaveSpacing) / 2f))); path.AddLine(pos.X - 7f, pos.Y, pos.X + 7f, pos.Y); path.AddLine(pos.X + 7f, pos.Y, pos.X + 7f, pos.Y + 5f); path.AddLine((float)(pos.X + 7f), (float)(pos.Y + 5f), (float)(pos.X - 7f), (float)(pos.Y + 5f)); path.AddLine(pos.X - 7f, pos.Y + 5f, pos.X - 7f, pos.Y); path.AddLine(pos.X - 7f, pos.Y, pos.X - 10f, pos.Y); path.AddLine(pos.X - 10f, pos.Y, pos.X - 10f, pos.Y - 1f); path.AddLine((float)(pos.X - 10f), (float)(pos.Y - 1f), (float)(pos.X + 10f), (float)(pos.Y - 1f)); path.AddLine(pos.X + 10f, pos.Y - 1f, pos.X + 10f, pos.Y); } } goto Label_0AD7; Label_06F6: pos = new RawVector2(pos.X - 2f, (pos.Y - (6f * (((float)McMeasure.StaveSpacing) / 2f))) + 3f); path.AddLine(pos.X, pos.Y, pos.X + 1.6f, pos.Y); path.AddLine(pos.X + 1.6f, pos.Y, (pos.X - 8f) + 2.4f, pos.Y + 19f); path.AddLine((float)((pos.X - 8f) + 2.4f), (float)(pos.Y + 19f), (float)((pos.X - 8f) - 0.8f), (float)(pos.Y + 19f)); path.AddLine((pos.X - 8f) - 0.8f, pos.Y + 19f, pos.X, pos.Y); DrawRestDotPart(renderTarget, pos, color); Label_0AD7: path.Flatten(); pathPoints = path.PathPoints; path.Dispose(); } PathGeometry geometry = new PathGeometry(factory2); if (pathPoints.Length > 1) { GeometrySink sink = geometry.Open(); sink.SetSegmentFlags(PathSegment.ForceRoundLineJoin); sink.BeginFigure(new RawVector2(pathPoints[0].X, pathPoints[0].Y), FigureBegin.Filled); for (int i = 1; i < pathPoints.Length; i++) { sink.AddLine(new RawVector2(pathPoints[i].X, pathPoints[i].Y)); } sink.EndFigure(FigureEnd.Closed); sink.Close(); sink.Dispose(); } SolidColorBrush brush = new SolidColorBrush(renderTarget, color.ToRawColor4(1f)); renderTarget.FillGeometry(geometry, brush); brush.Dispose(); geometry.Dispose(); renderTarget.EndDraw(); } catch (Exception) { factory.Dispose(); factory2.Dispose(); factory3.Dispose(); wicBitmap.Dispose(); renderTarget.Dispose(); return(null); } SharpDX.Direct2D1.Bitmap bitmap2 = SharpDX.Direct2D1.Bitmap.FromWicBitmap(renderTargetSource, wicBitmap); factory.Dispose(); factory2.Dispose(); factory3.Dispose(); wicBitmap.Dispose(); renderTarget.Dispose(); return(bitmap2); }
/// <summary> /// This gets called to instruct the element to draw itself in the appropriate spot of the graphics object /// </summary> /// <param name="g">The graphics object to draw to</param> /// <param name="printing">boolean, true if printing to the actual paper/document, false if drawing as a control</param> public override void Draw(Graphics g, bool printing) { GraphicsPath gp = new GraphicsPath(); Point[] pts; Matrix m = new Matrix(Size.Width / 100F, 0F, 0F, Size.Height / 100F, Location.X, Location.Y); m.RotateAt(_rotation, new PointF(50F, 50F), MatrixOrder.Prepend); Pen mypen = new Pen(_color, 2F); SolidBrush fillBrush = new SolidBrush(_color); mypen.LineJoin = LineJoin.Round; mypen.StartCap = LineCap.Round; mypen.EndCap = LineCap.Round; //All north arrows are defined as a graphics path in 100x100 size which is then scaled to fit the rectangle of the element switch (_northArrowStyle) { case (NorthArrowStyle.BlackArrow): gp.AddLine(50, 5, 5, 50); gp.AddLine(5, 50, 30, 50); gp.AddLine(30, 50, 30, 95); gp.AddLine(30, 95, 70, 95); gp.AddLine(70, 95, 70, 50); gp.AddLine(70, 50, 95, 50); gp.CloseFigure(); gp.Transform(m); g.DrawPath(mypen, gp); g.FillPath(fillBrush, gp); //N gp = new GraphicsPath(); gp.AddLine(40, 80, 40, 45); gp.StartFigure(); gp.AddLine(40, 45, 60, 80); gp.StartFigure(); gp.AddLine(60, 80, 60, 45); gp.StartFigure(); mypen.Color = Color.White; mypen.Width = Size.Width / 20; gp.Transform(m); g.DrawPath(mypen, gp); break; case (NorthArrowStyle.Default): //draw the outline DrawOutline(gp); gp.CloseFigure(); //Draw the N gp.AddLine(45, 57, 45, 43); gp.StartFigure(); gp.AddLine(45, 43, 55, 57); gp.StartFigure(); gp.AddLine(55, 57, 55, 43); gp.StartFigure(); gp.Transform(m); g.DrawPath(mypen, gp); //Draw the top arrow gp = new GraphicsPath(); gp.AddLine(50, 5, 60, 40); gp.AddBezier(60, 40, 55, 35, 45, 35, 40, 40); gp.CloseFigure(); gp.Transform(m); g.DrawPath(mypen, gp); g.FillPath(fillBrush, gp); break; case (NorthArrowStyle.CenterStar): //Outline DrawOutline(gp); gp.AddBezier(40F, 40F, 45F, 35F, 55F, 35F, 60f, 40F); gp.AddLine(60, 40, 50, 5); gp.CloseFigure(); //N gp.AddLine(47, 33, 47, 20); gp.StartFigure(); gp.AddLine(47, 20, 53, 33); gp.StartFigure(); gp.AddLine(53, 33, 53, 20); gp.StartFigure(); gp.Transform(m); g.DrawPath(mypen, gp); //Draw the center circle gp = new GraphicsPath(); gp.AddLine(30, 30, 40, 50); gp.AddLine(40, 50, 30, 70); gp.AddLine(30, 70, 50, 60); gp.AddLine(50, 60, 70, 70); gp.AddLine(70, 70, 60, 50); gp.AddLine(60, 50, 70, 30); gp.AddLine(70, 30, 50, 40); gp.CloseFigure(); gp.Transform(m); g.DrawPath(mypen, gp); g.FillPath(fillBrush, gp); break; case (NorthArrowStyle.TriangleN): pts = new Point[3]; pts[0] = new Point(50, 5); pts[1] = new Point(5, 95); pts[2] = new Point(95, 95); gp.AddLines(pts); gp.CloseFigure(); gp.AddLine(40, 80, 40, 45); gp.StartFigure(); gp.AddLine(40, 45, 60, 80); gp.StartFigure(); gp.AddLine(60, 80, 60, 45); gp.StartFigure(); gp.Transform(m); g.DrawPath(mypen, gp); break; case (NorthArrowStyle.TriangleHat): pts = new Point[3]; pts[0] = new Point(50, 5); pts[1] = new Point(5, 60); pts[2] = new Point(95, 60); gp.AddLines(pts); gp.CloseFigure(); gp.AddLine(5, 95, 5, 65); gp.StartFigure(); gp.AddLine(5, 65, 95, 95); gp.StartFigure(); gp.AddLine(95, 95, 95, 65); gp.StartFigure(); gp.Transform(m); g.DrawPath(mypen, gp); break; case (NorthArrowStyle.ArrowN): pts = new Point[3]; pts[0] = new Point(5, 25); pts[1] = new Point(50, 5); pts[2] = new Point(95, 25); gp.AddLines(pts); gp.CloseFigure(); gp.AddLine(50, 25, 50, 95); gp.StartFigure(); gp.AddLine(5, 80, 5, 45); gp.StartFigure(); gp.AddLine(5, 45, 95, 80); gp.StartFigure(); gp.AddLine(95, 80, 95, 45); gp.StartFigure(); gp.Transform(m); g.DrawPath(mypen, gp); break; } gp.Dispose(); }
public override void AddToPath(GraphicsPath graphicsPath) { graphicsPath.AddBezier(this.Start, this.FirstControlPoint, this.SecondControlPoint, this.End); }
private void Render(IGraphics ig) { string s = cbWhat.Text; if (string.IsNullOrEmpty(s)) { } else if (s == "Clipping") { Pen pn = new Pen(Color.LightGray, 5); Pen pn2 = new Pen(Color.Yellow); ig.Clear(Color.Black); GraphicsContainer cnt = ig.BeginContainer(); ig.SmoothingMode = SmoothingMode.HighQuality; ig.SetClip(new Rectangle(35, 35, 120, 120)); ig.DrawRectangle(pn, 5, 5, 45, 70); ig.DrawRectangle(pn, 15, 25, 90, 120); ig.DrawRectangle(pn, 50, 30, 100, 170); ig.DrawRectangle(pn, 5, 80, 180, 30); ig.DrawRectangle(pn, 75, 10, 40, 160); ig.EndContainer(cnt); ig.DrawRectangle(pn2, 5, 5, 45, 70); ig.DrawRectangle(pn2, 15, 25, 90, 120); ig.DrawRectangle(pn2, 50, 30, 100, 170); ig.DrawRectangle(pn2, 5, 80, 180, 30); ig.DrawRectangle(pn2, 75, 10, 40, 160); } else if (s == "Transforms") { ig.Clear(Color.Black); ig.RotateTransform(15); ig.DrawRectangle(new Pen(Color.Red, 2), 260, 80, 50, 40); ig.ResetTransform(); ig.DrawRectangle(new Pen(Color.Red, 2), 260, 80, 50, 40); ig.TranslateTransform(15, -5); GraphicsContainer cnt = ig.BeginContainer(); ig.SmoothingMode = SmoothingMode.HighQuality; ig.RotateTransform(5); ig.FillEllipse(new SolidBrush(Color.Orange), 100, 100, 80, 40); ig.DrawRectangle(new Pen(Color.Orange, 2), 60, 80, 40, 40); GraphicsContainer cnt2 = ig.BeginContainer(); ig.SmoothingMode = SmoothingMode.None; ig.RotateTransform(5); ig.ScaleTransform(1.1f, 1.2f); ig.FillEllipse(new SolidBrush(Color.YellowGreen), 130, 180, 80, 40); ig.DrawRectangle(new Pen(Color.YellowGreen, 2), 62, 80, 40, 40); GraphicsContainer cnt3 = ig.BeginContainer(); ig.SmoothingMode = SmoothingMode.HighQuality; Matrix mm = new Matrix(); mm.Shear(0.3f, 0f); ig.Transform = mm; ig.FillEllipse(new SolidBrush(Color.Green), 180, 120, 80, 40); ig.DrawRectangle(new Pen(Color.Green, 2), 62, 84, 40, 40); ig.EndContainer(cnt3); ig.EndContainer(cnt2); ig.FillEllipse(new SolidBrush(Color.Blue), 120, 150, 80, 40); ig.DrawRectangle(new Pen(Color.Blue, 2), 64, 80, 40, 40); ig.EndContainer(cnt); ig.FillEllipse(new SolidBrush(Color.Indigo), 80, 210, 80, 40); ig.DrawRectangle(new Pen(Color.Indigo, 2), 66, 80, 40, 40); ig.DrawRectangle(new Pen(Color.White, 2), 270, 30, 50, 40); ig.ResetTransform(); ig.DrawRectangle(new Pen(Color.White, 2), 270, 30, 50, 40); } else if (s == "Lines") { ig.SmoothingMode = SmoothingMode.AntiAlias; Pen ow = new Pen(Color.Purple, 12); ow.EndCap = LineCap.Round; ow.StartCap = LineCap.Round; ow.MiterLimit = 6f; ow.LineJoin = LineJoin.Miter; ig.SmoothingMode = SmoothingMode.None; Pen tp = new Pen(Color.Red, 2); tp.DashStyle = DashStyle.DashDot; ig.DrawLine(tp, 70, 20, 190, 20); tp.DashStyle = DashStyle.Dash; ig.DrawLine(tp, 70, 30, 190, 30); tp.DashStyle = DashStyle.Custom; tp.DashPattern = new float[] { 1, 8, 2, 2 }; ig.DrawLine(tp, 70, 40, 190, 40); ig.SmoothingMode = SmoothingMode.AntiAlias; PointF[] pts = new PointF[4]; pts[0] = new PointF(20, 50); pts[1] = new PointF(30, 90); pts[2] = new PointF(65, 60); pts[3] = new PointF(50, 40); ig.DrawLines(ow, pts); Point[] polly = new Point[] { new Point(200, 40), new Point(220, 140), new Point(240, 100), new Point(290, 70), new Point(230, 10) }; ig.DrawPolygon(tp, polly); //arrows Pen arr = new Pen(Color.DarkGoldenrod, 5); { arr.Width = 2; arr.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor; const float arrowWidth = 11.0f; // TUNE: const float arrowHeight = 14f; // TUNE: var arrowOutline = new System.Drawing.Drawing2D.GraphicsPath(); arrowOutline.AddLines(new PointF[] { new PointF(-(arrowWidth / 2), -arrowHeight), new PointF(0, 0), new PointF((arrowWidth / 2), -arrowHeight), new PointF(-(arrowWidth / 2), -arrowHeight) }); var generalizationArrow = new System.Drawing.Drawing2D.CustomLineCap(null, arrowOutline); generalizationArrow.SetStrokeCaps(System.Drawing.Drawing2D.LineCap.Round, System.Drawing.Drawing2D.LineCap.Round); generalizationArrow.BaseInset = arrowHeight; arr.CustomEndCap = generalizationArrow; ig.DrawLine(arr, 0, 120, 100, 200); } arr.Width = 5; AdjustableArrowCap aac = new AdjustableArrowCap(5, 3, false); arr.EndCap = LineCap.Custom; arr.CustomEndCap = aac; arr.StartCap = LineCap.ArrowAnchor; ig.DrawLine(arr, 50, 120, 150, 200); arr.Width = 7f; arr.EndCap = LineCap.RoundAnchor; arr.StartCap = LineCap.SquareAnchor; ig.DrawLine(arr, 100, 120, 200, 200); arr.Width = 9; arr.EndCap = LineCap.DiamondAnchor; arr.StartCap = LineCap.ArrowAnchor; ig.DrawLine(arr, 150, 120, 250, 200); Point[] al = new Point[] { new Point(200, 100), new Point(300, 200), new Point(300, 150) }; arr.Width = 9; arr.EndCap = LineCap.DiamondAnchor; arr.StartCap = LineCap.DiamondAnchor; ig.DrawLines(arr, al); } else if (s == "Curves") { PointF[] bezzie = new PointF[] { new PointF(20, 150), new PointF(110, 190), new PointF(120, 200), new PointF(50, 220), new PointF(60, 200), new PointF(140, 180), new PointF(100, 160), new PointF(180, 260), new PointF(200, 210), new PointF(190, 210) }; Pen bpn = new Pen(Color.MediumSeaGreen, 2); bpn.DashStyle = DashStyle.Custom; bpn.DashPattern = new float[] { 6, 1, 5, 2, 4, 3, 3, 4, 2, 5, 6, 1 }; ig.DrawBeziers(bpn, bezzie); PointF[] curvy = new PointF[] { new PointF(130, 40), new PointF(70, 70), new PointF(50, 20), new PointF(120, 120), new PointF(150, 80), new PointF(80, 150), new PointF(80, 110) }; ig.DrawCurve(new Pen(Color.Blue, 5), curvy); ig.DrawCurve(new Pen(Color.Red, 2), curvy, 2, 3); ig.DrawCurve(new Pen(Color.Yellow, 1), curvy, 1f); Point[] ccurvy = new Point[] { new Point(280, 30), new Point(260, 60), new Point(200, 20), new Point(290, 120), new Point(290, 80), new Point(230, 150), new Point(150, 50) }; ig.DrawClosedCurve(new Pen(Color.Green, 3), ccurvy, 1f, FillMode.Alternate); ig.DrawClosedCurve(new Pen(Color.Purple, 1), ccurvy, 0f, FillMode.Alternate); Point[] fcc = new Point[] { new Point(160, 350), new Point(190, 370), new Point(130, 390), new Point(190, 400), new Point(195, 410), new Point(100, 430), new Point(160, 450) }; ig.FillClosedCurve(new SolidBrush(Color.Red), fcc, FillMode.Winding, 1f); ig.FillClosedCurve(new SolidBrush(Color.Aquamarine), fcc, FillMode.Alternate, .2f); } else if (s == "Transparency") { Point[] fillpoly = new Point[] { new Point(20, 130), new Point(60, 90), new Point(30, 20), new Point(80, 20), new Point(15, 90), new Point(100, 50), new Point(0, 50) }; Color col = Color.FromArgb(96, 255, 0, 0); ig.FillEllipse(new SolidBrush(Color.Ivory), 60, 140, 60, 30); ig.FillPolygon(new SolidBrush(Color.Ivory), fillpoly, FillMode.Winding); ig.TranslateTransform(10, 10); ig.FillEllipse(new SolidBrush(col), 60, 140, 60, 30); ig.FillPolygon(new SolidBrush(col), fillpoly, FillMode.Alternate); ig.ResetTransform(); ig.FillPie(new SolidBrush(Color.FromArgb(100, 255, 0, 0)), 10, 200, 200, 80, 315, 90); ig.FillPie(new SolidBrush(Color.FromArgb(100, 128, 128, 0)), 10, 200, 200, 80, 250, -90); ig.FillPie(new SolidBrush(Color.FromArgb(100, 128, 0, 128)), 15, 205, 190, 70, 180, 270); ig.FillPie(new SolidBrush(Color.FromArgb(100, 200, 60, 60)), 20, 210, 180, 60, 45, -270); } else if (s == "Fills") { LinearGradientBrush gbr1 = new LinearGradientBrush(new Point(0, 0), new Point(30, 20), Color.Blue, Color.Plum); ColorBlend blend = new ColorBlend(3); blend.Colors = new Color[] { Color.Red, Color.Yellow, Color.MediumSlateBlue }; blend.Positions = new float[] { 0, .3f, 1f }; gbr1.InterpolationColors = blend; Point[] sp = new Point[] { new Point(145, 145), new Point(305, 250), new Point(220, 250), new Point(180, 250) }; ig.FillPolygon(gbr1, sp); LinearGradientBrush gbr2 = new LinearGradientBrush(new Point(0, 0), new Point(10, 20), Color.WhiteSmoke, Color.CornflowerBlue); gbr2.WrapMode = WrapMode.TileFlipXY; Point[] sp2 = new Point[] { new Point(25, 205), new Point(75, 150), new Point(110, 110), new Point(40, 80) }; ig.FillPolygon(gbr2, sp2); ig.FillRectangle(new HatchBrush(HatchStyle.DiagonalBrick, Color.Khaki, Color.Peru), 000, 5, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.Vertical, Color.Bisque, Color.Peru), 020, 5, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.DarkVertical, Color.Tan, Color.Peru), 040, 5, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.DiagonalCross, Color.Chocolate, Color.Peru), 060, 5, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.WideDownwardDiagonal, Color.BurlyWood, Color.Peru), 080, 5, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.LargeConfetti, Color.Wheat, Color.Peru), 100, 5, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.ZigZag, Color.SaddleBrown, Color.Peru), 120, 5, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.HorizontalBrick, Color.Linen, Color.Peru), 140, 5, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.LightHorizontal, Color.Maroon, Color.Peru), 160, 5, 20, 20); ig.FillRectangle(gbr1, 200, 5, 20, 20); ig.FillRectangle(gbr2, 220, 5, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.Percent05, Color.CornflowerBlue, Color.LemonChiffon), 000, 30, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.Percent10, Color.CornflowerBlue, Color.LemonChiffon), 020, 30, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.Percent20, Color.CornflowerBlue, Color.LemonChiffon), 040, 30, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.Percent25, Color.CornflowerBlue, Color.LemonChiffon), 060, 30, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.Percent30, Color.CornflowerBlue, Color.LemonChiffon), 080, 30, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.Percent40, Color.CornflowerBlue, Color.LemonChiffon), 100, 30, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.Percent50, Color.CornflowerBlue, Color.LemonChiffon), 120, 30, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.Percent60, Color.CornflowerBlue, Color.LemonChiffon), 140, 30, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.Percent70, Color.CornflowerBlue, Color.LemonChiffon), 160, 30, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.Percent75, Color.CornflowerBlue, Color.LemonChiffon), 180, 30, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.Percent80, Color.CornflowerBlue, Color.LemonChiffon), 200, 30, 20, 20); ig.FillRectangle(new HatchBrush(HatchStyle.Percent90, Color.CornflowerBlue, Color.LemonChiffon), 220, 30, 20, 20); } else if (s == "Arcs/Pies") { //GDI does not seem to draw arcs correctly except when the ellipse is a circle. //These arcs demonstrate the problem. SVGGraphics calculates arcs correctly. ig.DrawArc(new Pen(Color.Black, 2), 120 + 5 * 3, 120, 110 * 3, 110, 0, 240); ig.DrawArc(new Pen(Color.Black, 2), 120 + 10 * 3, 125, 100 * 3, 100, 0, 210); ig.DrawArc(new Pen(Color.Black, 2), 120 + 15 * 3, 130, 90 * 3, 90, 0, 180); ig.DrawArc(new Pen(Color.Black, 2), 120 + 20 * 3, 135, 80 * 3, 80, 0, 150); ig.DrawArc(new Pen(Color.Black, 2), 120 + 25 * 3, 140, 70 * 3, 70, 0, 120); ig.DrawArc(new Pen(Color.Black, 2), 120 + 30 * 3, 145, 60 * 3, 60, 0, 90); ig.DrawArc(new Pen(Color.Black, 2), 120 + 35 * 3, 150, 50 * 3, 50, 0, 60); ig.DrawArc(new Pen(Color.Black, 2), 120 + 40 * 3, 155, 40 * 3, 40, 0, 270); ig.DrawPie(new Pen(Color.Pink, 2), 110, 50, 100, 100, 315, 90); ig.DrawPie(new Pen(Color.Purple, 2), 110, 50, 100, 100, 250, -90); ig.DrawPie(new Pen(Color.DarkRed, 2), 115, 55, 90, 90, 180, 270); ig.DrawPie(new Pen(Color.Red, 2), 120, 60, 80, 80, 45, -270); } else if (s == "Text") { Font fnt1 = new Font("Helvetica", 12, FontStyle.Italic | FontStyle.Bold); Font fnt2 = new Font(FontFamily.GenericMonospace, 16, FontStyle.Bold); Font fnt3 = new Font("", 40, FontStyle.Underline); Rectangle rc1 = new Rectangle(30, 30, 220, 20); StringFormat fmt1 = new StringFormat(); fmt1.Alignment = StringAlignment.Near; ig.DrawRectangle(new Pen(Color.Blue), rc1); ig.DrawString("Text...1", fnt1, new SolidBrush(Color.DarkGreen), rc1, fmt1); Rectangle rc2 = new Rectangle(0, 0, 120, 20); StringFormat fmt2 = new StringFormat(); fmt2.Alignment = StringAlignment.Center; ig.TranslateTransform(30, 160); ig.RotateTransform(90); ig.DrawRectangle(new Pen(Color.Blue), rc2); ig.DrawString("Text...2", fnt2, new SolidBrush(Color.DarkGreen), rc2, fmt2); ig.ResetTransform(); Rectangle rc3 = new Rectangle(30, 90, 300, 30); StringFormat fmt3 = new StringFormat(); fmt3.Alignment = StringAlignment.Far; ig.DrawRectangle(new Pen(Color.Blue), rc3); ig.DrawString("Text...3", fnt3, new SolidBrush(Color.DarkGreen), rc3, fmt3); //measurestring const string mme = "MeasureString Is Impossible To Emulate"; SizeF siz = ig.MeasureString(mme, fnt1); ig.DrawRectangle(new Pen(Color.Red), 20, 200, siz.Width, siz.Height); siz = ig.MeasureString(mme, fnt1, 150); ig.DrawRectangle(new Pen(Color.Orange), 20, 230, siz.Width, siz.Height); siz = ig.MeasureString(mme, fnt1, new SizeF(150, 150), new StringFormat(StringFormatFlags.DirectionVertical)); ig.DrawRectangle(new Pen(Color.Yellow), 20, 200, siz.Width, siz.Height); } else if (s == "Rect-aligned Text") { ig.Clear(Color.White); ig.ScaleTransform( (float)panel1.ClientSize.Width / RectAlignedTextTest.CanvasSize, (float)panel1.ClientSize.Height / RectAlignedTextTest.CanvasSize); RectAlignedTextTest.DrawTest(ig); } else if (s == "Images") { Icon ike = new Icon(GetType(), "App.ico"); ig.DrawIcon(ike, 10, 10); //ig.DrawIcon(ike, new Rectangle(270, 400, 30, 40)); Bitmap bmp = new Bitmap(GetType(), "test.bmp"); ig.DrawImage(bmp, 100f, 150f); GraphicsContainer cnt = ig.BeginContainer(); ig.RotateTransform(5); ig.DrawImage(bmp, 160f, 50f, 120f, 70f); ig.EndContainer(cnt); //ig.DrawImageUnscaled(bmp, 270, 450, 20, 20); } else if (s == "Path") { /* The following example GraphicsPath code comes from the MSDN docs on the GraphicsPathIterator class * https://msdn.microsoft.com/en-us/library/79k451ts.aspx * */ // Create a graphics path. GraphicsPath myPath = new GraphicsPath(); // Set up primitives to add to myPath. Point[] myPoints = { new Point(20, 20), new Point(120, 120), new Point(20, 120), new Point(20, 20) }; Rectangle myRect = new Rectangle(120, 120, 100, 100); // Add 3 lines, a rectangle, an ellipse, and 2 markers. myPath.AddLines(myPoints); myPath.SetMarkers(); myPath.AddRectangle(myRect); myPath.SetMarkers(); myPath.AddEllipse(220, 220, 100, 100); ig.DrawPath(new Pen(Color.Black), myPath); LinearGradientBrush gbr2 = new LinearGradientBrush(new Point(0, 0), new Point(10, 20), Color.WhiteSmoke, Color.CornflowerBlue); gbr2.WrapMode = WrapMode.TileFlipXY; ig.FillPath(gbr2, myPath); GraphicsPath myPath2 = new GraphicsPath(); myPath2.AddLine(100, 100, 130, 120); myPath2.AddEllipse(120, 120, 120, 140); myPath2.AddBezier(130, 160, 170, 160, 150, 130, 200, 110); ig.DrawPath(new Pen(Color.Blue), myPath2); } else if (s == "Path 2 (Slow)") { SolidBrush mySolidBrush = new SolidBrush(Color.Aqua); GraphicsPath myGraphicsPath = new GraphicsPath(); Point[] myPointArray = { new Point(15, 20), new Point(20, 40), new Point(50, 30) }; FontFamily myFontFamily = new FontFamily("Times New Roman"); PointF myPointF = new PointF(50, 20); StringFormat myStringFormat = new StringFormat(); myGraphicsPath.AddArc(0, 0, 30, 20, -90, 180); myGraphicsPath.AddCurve(myPointArray); myGraphicsPath.AddString("a string in a path filled", myFontFamily, 0, 24, myPointF, myStringFormat); myGraphicsPath.AddPie(230, 10, 40, 40, 40, 110); ig.FillPath(mySolidBrush, myGraphicsPath); ig.DrawPath(new Pen(Color.Green), myGraphicsPath); } else { throw new NotImplementedException(); } }
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); } } }
private GraphicsPath BuildMenuIconShape(ref Rectangle rcMenuIcon) { GraphicsPath XMenuIconPath = new GraphicsPath(); switch (m_xTitleBar.TitleBarType) { case XTitleBar.XTitleBarType.Rounded: XMenuIconPath.AddArc( rcMenuIcon.Left, rcMenuIcon.Top, rcMenuIcon.Height, rcMenuIcon.Height, 90, 180); XMenuIconPath.AddLine( rcMenuIcon.Left + rcMenuIcon.Height / 2, rcMenuIcon.Top, rcMenuIcon.Right, rcMenuIcon.Top ); XMenuIconPath.AddBezier( new Point(rcMenuIcon.Right, rcMenuIcon.Top), new Point(rcMenuIcon.Right - 10, rcMenuIcon.Bottom / 2 - 5), new Point(rcMenuIcon.Right - 12, rcMenuIcon.Bottom / 2 + 5), new Point(rcMenuIcon.Right, rcMenuIcon.Bottom) ); XMenuIconPath.AddLine( rcMenuIcon.Right, rcMenuIcon.Bottom, rcMenuIcon.Left + rcMenuIcon.Height / 2, rcMenuIcon.Bottom ); break; case XTitleBar.XTitleBarType.Angular: XMenuIconPath.AddArc( rcMenuIcon.Left, rcMenuIcon.Top, rcMenuIcon.Height, rcMenuIcon.Height, 90, 180); XMenuIconPath.AddLine( rcMenuIcon.Left + rcMenuIcon.Height / 2, rcMenuIcon.Top, rcMenuIcon.Right + 18, rcMenuIcon.Top ); XMenuIconPath.AddLine( rcMenuIcon.Right + 18, rcMenuIcon.Top, rcMenuIcon.Right - 5, rcMenuIcon.Bottom ); XMenuIconPath.AddLine( rcMenuIcon.Right - 5, rcMenuIcon.Bottom, rcMenuIcon.Left + rcMenuIcon.Height / 2, rcMenuIcon.Bottom ); break; case XTitleBar.XTitleBarType.Rectangular: XMenuIconPath.AddArc( rcMenuIcon.Left, rcMenuIcon.Top, rcMenuIcon.Height, rcMenuIcon.Height, 90, 180); XMenuIconPath.AddLine( rcMenuIcon.Left + rcMenuIcon.Height / 2, rcMenuIcon.Top, rcMenuIcon.Right, rcMenuIcon.Top ); XMenuIconPath.AddLine( rcMenuIcon.Right, rcMenuIcon.Top, rcMenuIcon.Right, rcMenuIcon.Bottom ); XMenuIconPath.AddLine( rcMenuIcon.Right, rcMenuIcon.Bottom, rcMenuIcon.Left + rcMenuIcon.Height / 2, rcMenuIcon.Bottom ); break; } return(XMenuIconPath); }
public static void DrawTab(Graphics g, Rectangle r, Corners corner, GradientType gradient, Color darkColor, Color lightColor, Color edgeColor, bool closed) { //dims Point[] points; GraphicsPath path; Region Region; LinearGradientBrush linearBrush; Brush brush = null; Pen pen; r.Inflate(- 1, - 1); //set brushes switch (gradient) { case GradientType.Flat: brush = new SolidBrush(darkColor); break; case GradientType.Linear: brush = new LinearGradientBrush(r, darkColor, lightColor, LinearGradientMode.Vertical); break; case GradientType.Bell: linearBrush = new LinearGradientBrush(r, darkColor, lightColor, LinearGradientMode.Vertical); linearBrush.SetSigmaBellShape((float) (0.17F), (float) (0.67F)); brush = linearBrush; break; } pen = new pen(edgeColor, 1); //generic points points = new Point[12] {new Point(r.Left, r.Bottom), new Point(r.Left, r.Bottom - bshift), new Point(r.Left, r.Top + bshift), new Point(r.Left, r.Top), new Point(r.Left + bshift, r.Top), new Point(r.Right - bshift, r.Top), new Point(r.Right, r.Top), new Point(r.Right, r.Top + bshift), new Point(r.Right, r.Bottom - bshift), new Point(r.Right, r.Bottom), new Point(r.Right - bshift, r.Bottom), new Point(r.Left + bshift, r.Bottom)}; path = new GraphicsPath(); switch (corner) { case Corners.LeftBottom: path.AddLine(points[3], points[1]); path.AddBezier(points[1], points[0], points[0], points[11]); path.AddLine(points[11], points[9]); path.AddLine(points[9], points[6]); path.AddLine(points[6], points[3]); Region = new Region(path); g.FillRegion(brush, Region); g.DrawLine(pen, points[3], points[1]); g.DrawBezier(pen, points[1], points[0], points[0], points[11]); g.DrawLine(pen, points[11], points[9]); g.DrawLine(pen, points[9], points[6]); if (closed) { g.DrawLine(pen, points[6], points[3]); } break; case Corners.LeftTop: path.AddLine(points[0], points[2]); path.AddBezier(points[2], points[3], points[3], points[4]); path.AddLine(points[4], points[6]); path.AddLine(points[6], points[9]); path.AddLine(points[9], points[0]); Region = new Region(path); g.FillRegion(brush, Region); g.DrawLine(pen, points[0], points[2]); g.DrawBezier(pen, points[2], points[3], points[3], points[4]); g.DrawLine(pen, points[4], points[6]); g.DrawLine(pen, points[6], points[9]); if (closed) { g.DrawLine(pen, points[9], points[0]); } break; case Corners.Bottom: path.AddLine(points[1], points[3]); path.AddBezier(points[1], points[0], points[0], points[11]); path.AddLine(points[11], points[10]); path.AddBezier(points[10], points[9], points[9], points[8]); path.AddLine(points[8], points[6]); path.AddLine(points[6], points[3]); Region = new Region(path); g.FillRegion(brush, Region); g.DrawLine(pen, points[1], points[3]); g.DrawBezier(pen, points[1], points[0], points[0], points[11]); g.DrawLine(pen, points[11], points[10]); g.DrawBezier(pen, points[10], points[9], points[9], points[8]); g.DrawLine(pen, points[8], points[6]); if (closed) { g.DrawLine(pen, points[6], points[3]); } break; case Corners.Top: path.AddLine(points[0], points[2]); path.AddBezier(points[2], points[3], points[3], points[4]); path.AddLine(points[4], points[5]); path.AddBezier(points[5], points[6], points[6], points[7]); path.AddLine(points[7], points[9]); path.AddLine(points[9], points[0]); Region = new Region(path); g.FillRegion(brush, Region); g.DrawLine(pen, points[0], points[2]); g.DrawBezier(pen, points[2], points[3], points[3], points[4]); g.DrawLine(pen, points[4], points[5]); g.DrawBezier(pen, points[5], points[6], points[6], points[7]); g.DrawLine(pen, points[7], points[9]); if (closed) { g.DrawLine(pen, points[9], points[0]); } break; case Corners.RightBottom: path.AddLine(points[3], points[0]); path.AddLine(points[0], points[10]); path.AddBezier(points[10], points[9], points[9], points[8]); path.AddLine(points[8], points[6]); path.AddLine(points[6], points[3]); Region = new Region(path); g.FillRegion(brush, Region); g.DrawLine(pen, points[3], points[0]); g.DrawLine(pen, points[0], points[10]); g.DrawBezier(pen, points[10], points[9], points[9], points[8]); g.DrawLine(pen, points[8], points[6]); if (closed) { g.DrawLine(pen, points[6], points[3]); } break; case Corners.RightTop: path.AddLine(points[0], points[3]); path.AddLine(points[3], points[5]); path.AddBezier(points[5], points[6], points[6], points[7]); path.AddLine(points[7], points[9]); path.AddLine(points[9], points[0]); Region = new Region(path); g.FillRegion(brush, Region); g.DrawLine(pen, points[0], points[3]); g.DrawLine(pen, points[3], points[5]); g.DrawBezier(pen, points[5], points[6], points[6], points[7]); g.DrawLine(pen, points[7], points[9]); if (closed) { g.DrawLine(pen, points[9], points[0]); } break; } }
public ConsoleColoredString Validate() { if (SvgPath == null && (Filename == null || Id == null)) { return(CommandLineParser.Colorize(RhoML.Parse("If {option}-p{} is not used, {option}-f{} and {option}-i{} must both be present."))); } if (SvgPath != null && (Filename != null || Id != null)) { return(CommandLineParser.Colorize(RhoML.Parse("If {option}-p{} is used, neither {option}-f{} nor {option}-i{} must be present."))); } Matrix matrix = new Matrix(); if (Filename != null) { if (!File.Exists(Filename)) { return(CommandLineParser.Colorize(RhoML.Parse("The specified file, {{h}}{0}{{}}, does not exist.".Fmt(Filename)))); } var xml = XDocument.Parse(File.ReadAllText(Filename)); var element = xml.Descendants().FirstOrDefault(el => el.AttributeI("id").NullOr(id => id.Value == Id) == true); if (element == null) { return(CommandLineParser.Colorize(RhoML.Parse("An XML element with the specified ID, {{h}}{0}{{}}, does not exist in the specified file.".Fmt(Id)))); } SvgPath = element.AttributeI("d")?.Value; if (SvgPath == null) { return(CommandLineParser.Colorize(RhoML.Parse("The XML element with the specified ID, {{h}}{0}{{}}, does not have the {{h}}d{{}} attribute.".Fmt(Id)))); } var tr = element.AttributeI("transform")?.Value; Match m; if (tr != null && (m = Regex.Match(tr, @"^\s*matrix\s*\(\s*(-?\d*(?:\d|\.\d+)),\s*(-?\d*(?:\d|\.\d+)),\s*(-?\d*(?:\d|\.\d+)),\s*(-?\d*(?:\d|\.\d+)),\s*(-?\d*(?:\d|\.\d+)),\s*(-?\d*(?:\d|\.\d+))\s*\)\s*$")).Success) { matrix = new Matrix(float.Parse(m.Groups[1].Value), float.Parse(m.Groups[2].Value), float.Parse(m.Groups[3].Value), float.Parse(m.Groups[4].Value), float.Parse(m.Groups[5].Value), float.Parse(m.Groups[6].Value)); System.Console.WriteLine("Using matrix!"); } } PointF?prev = null; bool figure = false; SvgPath = SvgPath.Trim(); var num = @"-?\d*(?:\d|\.\d+)(?=$|\s|,)"; ParsedPath = new GraphicsPath(UseWindingFill ? FillMode.Winding : FillMode.Alternate); var index = 0; char?prevCommand = null; var commands = "MLCZz"; while (index < SvgPath.Length) { var match = Regex.Match(SvgPath.Substring(index), @"^(?: [ML]{1}(?:\s|,)*({0})(?:\s|,)+({0})(?:\s|,)*| [C]{2}(?:\s|,)*({0})(?:\s|,)+({0})(?:\s|,)+({0})(?:\s|,)+({0})(?:\s|,)+({0})(?:\s|,)+({0})(?:\s|,)*| [Zz](?:\s|,)* )".Fmt( /* {0} */ num, /* {1} */ prevCommand == 'M' || prevCommand == 'L' ? "?" : "", /* {2} */ prevCommand == 'C' ? "?" : "" ), RegexOptions.IgnorePatternWhitespace); if (!match.Success) { return("The specified path data does not conform to the expected syntax at index {0}.".Fmt(index)); } index += match.Length; if (match.Value[0] != 'M' && prev.Value == null) { return("The path data cannot start with any command other than M."); } if (commands.Contains(match.Value[0])) { prevCommand = match.Value[0]; } switch (prevCommand) { case 'M': if (figure) { ParsedPath.CloseFigure(); } prev = new PointF(float.Parse(match.Groups[1].Value), float.Parse(match.Groups[2].Value)); break; case 'L': var p = new PointF(float.Parse(match.Groups[1].Value), float.Parse(match.Groups[2].Value)); ParsedPath.AddLine(prev.Value, p); prev = p; figure = true; break; case 'C': var p1 = new PointF(float.Parse(match.Groups[3].Value), float.Parse(match.Groups[4].Value)); var p2 = new PointF(float.Parse(match.Groups[5].Value), float.Parse(match.Groups[6].Value)); var p3 = new PointF(float.Parse(match.Groups[7].Value), float.Parse(match.Groups[8].Value)); ParsedPath.AddBezier(prev.Value, p1, p2, p3); prev = p3; figure = true; break; case 'Z': case 'z': if (figure) { ParsedPath.CloseFigure(); figure = false; } break; default: return("The specified path data does not conform to the expected syntax. (2)"); } } if (figure) { ParsedPath.CloseFigure(); } ParsedPath.Transform(matrix); return(null); }