示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static byte[] GetCaptcha1()
        {
            var width  = 60;
            var height = 40;

            using (Image img = new Image <Rgba32>(width, height))
            {
                PathBuilder pathBuilder = new PathBuilder();
                pathBuilder.SetOrigin(new PointF(0, 0));
                var list = new List <PointF>();
                for (int i = 0; i < 4; i++)
                {
                    var widthPointF  = new PointF(new Random().Next(1, width), new Random().Next(1, width));
                    var heightPointF = new PointF(new Random().Next(1, height), new Random().Next(1, height));
                    list.Add(widthPointF);
                    list.Add(heightPointF);
                }
                pathBuilder.AddSegment(new LinearLineSegment(list.ToArray()));



                IPath path = pathBuilder.Build();

                var    font = SystemFonts.CreateFont("Arial", 36F, FontStyle.Regular);
                string text = "1234";
                var    textGraphicsOptions = new TextGraphicsOptions() // draw the text along the path wrapping at the end of the line
                {
                    TextOptions =
                    {
                        WrapTextWidth = width
                    }
                };

                var glyphs = TextBuilder.GenerateGlyphs(text, path, new RendererOptions(font, textGraphicsOptions.TextOptions.DpiX, textGraphicsOptions.TextOptions.DpiY)
                {
                    HorizontalAlignment = textGraphicsOptions.TextOptions.HorizontalAlignment,
                    TabWidth            = textGraphicsOptions.TextOptions.TabWidth,
                    VerticalAlignment   = textGraphicsOptions.TextOptions.VerticalAlignment,
                    WrappingWidth       = textGraphicsOptions.TextOptions.WrapTextWidth,
                    ApplyKerning        = textGraphicsOptions.TextOptions.ApplyKerning
                });

                img.Mutate(ctx => ctx
                           .Fill(Color.White)         // white background image
                           .Draw(Color.Gray, 3, path) // draw the path so we can see what the text is supposed to be following
                           .Fill(Color.Black, glyphs));

                //img.Save("output/wordart100.png");
                using (var ms = new MemoryStream())
                {
                    img.SaveAsGif(ms);
                    return(ms.ToArray());
                }
            }
        }
示例#2
0
 // NB: this does not support passing the builder's transformation to the arc...
 private static PathBuilder AddEllipticalArc(this PathBuilder builder, float x, float y, float radiusX, float radiusY, float rotation, float startAngle, float sweepAngle) =>
 builder.AddSegment(new EllipticalArcLineSegment(x, y, radiusX, radiusY, rotation, startAngle, sweepAngle, Matrix3x2.Identity));