public void ImageShouldBeOverlayedByPath() { string path = this.CreateOutputDirectory("Drawing", "Path"); using (Image <Rgba32> image = new Image <Rgba32>(500, 500)) { LinearLineSegment linerSegemnt = new LinearLineSegment( new Vector2(10, 10), new Vector2(200, 150), new Vector2(50, 300)); BezierLineSegment bazierSegment = new BezierLineSegment(new Vector2(50, 300), new Vector2(500, 500), new Vector2(60, 10), new Vector2(10, 400)); ShapePath p = new ShapePath(linerSegemnt, bazierSegment); using (FileStream output = File.OpenWrite($"{path}/Simple.png")) { image .BackgroundColor(Rgba32.Blue) .Draw(Rgba32.HotPink, 5, p) .Save(output); } using (PixelAccessor <Rgba32> sourcePixels = image.Lock()) { Assert.Equal(Rgba32.HotPink, sourcePixels[9, 9]); Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]); Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); } } }
public void ImageShouldBeOverlayedByPath() { string path = TestEnvironment.CreateOutputDirectory("Drawing", "Path"); using (Image <Rgba32> image = new Image <Rgba32>(500, 500)) { LinearLineSegment linerSegemnt = new LinearLineSegment( new Vector2(10, 10), new Vector2(200, 150), new Vector2(50, 300)); CubicBezierLineSegment bazierSegment = new CubicBezierLineSegment(new Vector2(50, 300), new Vector2(500, 500), new Vector2(60, 10), new Vector2(10, 400)); ShapePath p = new ShapePath(linerSegemnt, bazierSegment); image.Mutate(x => x .BackgroundColor(Rgba32.Blue) .Draw(Rgba32.HotPink, 5, p)); image.Save($"{path}/Simple.png"); using (PixelAccessor <Rgba32> sourcePixels = image.Lock()) { Assert.Equal(Rgba32.HotPink, sourcePixels[11, 11]); Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]); Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); } } }
public void CorrectlySetsPenPointsAndOptions() { img.DrawBeziers(pen, points, noneDefault); Assert.NotEmpty(img.ProcessorApplications); DrawPathProcessor <Color> processor = Assert.IsType <DrawPathProcessor <Color> >(img.ProcessorApplications[0].processor); Assert.Equal(noneDefault, processor.Options); ShapePath path = Assert.IsType <ShapePath>(processor.Path); SixLabors.Shapes.Path vector = Assert.IsType <SixLabors.Shapes.Path>(path.Path); BezierLineSegment segment = Assert.IsType <BezierLineSegment>(vector.LineSegments[0]); Assert.Equal(pen, processor.Pen); }
public void CorrectlySetsPenAndPoints() { img.DrawLines(pen, points); Assert.NotEmpty(img.ProcessorApplications); DrawPathProcessor <Rgba32> processor = Assert.IsType <DrawPathProcessor <Rgba32> >(img.ProcessorApplications[0].processor); Assert.Equal(GraphicsOptions.Default, processor.Options); ShapePath path = Assert.IsType <ShapePath>(processor.Path); SixLabors.Shapes.Path vector = Assert.IsType <SixLabors.Shapes.Path>(path.Path); LinearLineSegment segment = Assert.IsType <LinearLineSegment>(vector.LineSegments[0]); Assert.Equal(pen, processor.Pen); }
/// <summary> /// Generate normal texture from height map. /// Note : heightMap should be in projected coordinates (see ReprojectToCartesian()) /// </summary> /// <param name="heightMap">heightMap in projected coordinates</param> /// <param name="outputDirectory"></param> /// <returns></returns> public TextureInfo GenerateNormalMap(HeightMap heightMap, string outputDirectory, string fileName = "normalmap.png") { List <Vector3> normals = _meshService.ComputeNormals(heightMap).ToList(); #if NETSTANDARD using (Image <Bgra32> outputImage = new Image <Bgra32>(heightMap.Width, heightMap.Height)) { for (int j = 0; j < heightMap.Height; j++) { for (int i = 0; i < heightMap.Width; i++) { int index = i + (j * heightMap.Width); Vector3 norm = normals[index]; Bgra32 color = FromVec3NormalToColor(norm); outputImage[i, j] = color; } } outputImage.Save(System.IO.Path.Combine(outputDirectory, fileName)); } #elif NETFULL using (var dbm = new DirectBitmap(heightMap.Width, heightMap.Height)) { for (int j = 0; j < heightMap.Height; j++) { for (int i = 0; i < heightMap.Width; i++) { int index = i + (j * heightMap.Width); Vector3 norm = normals[index]; Color color = FromVec3NormalToColor(norm); dbm.SetPixel(i, j, color); } } dbm.Bitmap.Save(Path.Combine(outputDirectory, fileName), ImageFormat.Png); } #endif TextureInfo normal = new TextureInfo(System.IO.Path.Combine(outputDirectory, fileName), TextureImageFormat.image_jpeg, heightMap.Width, heightMap.Height); return(normal); }
public void CorrectlySetsBrushThicknessPointsAndOptions() { img.DrawLines(brush, thickness, points, noneDefault); Assert.NotEmpty(img.ProcessorApplications); DrawPathProcessor <Rgba32> processor = Assert.IsType <DrawPathProcessor <Rgba32> >(img.ProcessorApplications[0].processor); Assert.Equal(noneDefault, processor.Options); ShapePath path = Assert.IsType <ShapePath>(processor.Path); SixLabors.Shapes.Path vector = Assert.IsType <SixLabors.Shapes.Path>(path.Path); LinearLineSegment segment = Assert.IsType <LinearLineSegment>(vector.LineSegments[0]); Pen <Rgba32> pen = Assert.IsType <Pen <Rgba32> >(processor.Pen); Assert.Equal(brush, pen.Brush); Assert.Equal(thickness, pen.Width); }
public void ImageShouldBeOverlayedPathWithOpacity() { string path = this.CreateOutputDirectory("Drawing", "Path"); Rgba32 color = new Rgba32(Rgba32.HotPink.R, Rgba32.HotPink.G, Rgba32.HotPink.B, 150); LinearLineSegment linerSegemnt = new LinearLineSegment( new Vector2(10, 10), new Vector2(200, 150), new Vector2(50, 300) ); BezierLineSegment bazierSegment = new BezierLineSegment(new Vector2(50, 300), new Vector2(500, 500), new Vector2(60, 10), new Vector2(10, 400)); ShapePath p = new ShapePath(linerSegemnt, bazierSegment); using (Image <Rgba32> image = new Image <Rgba32>(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Opacity.png")) { image .BackgroundColor(Rgba32.Blue) .Draw(color, 10, p) .Save(output); } //shift background color towards forground color by the opacity amount Rgba32 mergedColor = new Rgba32(Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); using (PixelAccessor <Rgba32> sourcePixels = image.Lock()) { Assert.Equal(mergedColor, sourcePixels[9, 9]); Assert.Equal(mergedColor, sourcePixels[199, 149]); Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); } } }
public void CorrectlySetsBrushThicknessAndPoints() { img.DrawBeziers(brush, thickness, points); Assert.NotEmpty(img.ProcessorApplications); DrawPathProcessor <Color> processor = Assert.IsType <DrawPathProcessor <Color> >(img.ProcessorApplications[0].processor); Assert.Equal(GraphicsOptions.Default, processor.Options); ShapePath path = Assert.IsType <ShapePath>(processor.Path); Assert.NotEmpty(path.Paths); SixLabors.Shapes.Path vector = Assert.IsType <SixLabors.Shapes.Path>(path.Paths[0]); BezierLineSegment segment = Assert.IsType <BezierLineSegment>(vector.LineSegments[0]); Pen <Color> pen = Assert.IsType <Pen <Color> >(processor.Pen); Assert.Equal(brush, pen.Brush); Assert.Equal(thickness, pen.Width); }
public void CorrectlySetsColorThicknessPointsAndOptions() { img.DrawBeziers(color, thickness, points, noneDefault); Assert.NotEmpty(img.ProcessorApplications); DrawPathProcessor <Color> processor = Assert.IsType <DrawPathProcessor <Color> >(img.ProcessorApplications[0].processor); Assert.Equal(noneDefault, processor.Options); ShapePath path = Assert.IsType <ShapePath>(processor.Path); SixLabors.Shapes.Path vector = Assert.IsType <SixLabors.Shapes.Path>(path.Path); BezierLineSegment segment = Assert.IsType <BezierLineSegment>(vector.LineSegments[0]); Pen <Color> pen = Assert.IsType <Pen <Color> >(processor.Pen); Assert.Equal(thickness, pen.Width); SolidBrush <Color> brush = Assert.IsType <SolidBrush <Color> >(pen.Brush); Assert.Equal(color, brush.Color); }
public void CorrectlySetsColorThicknessAndPoints() { img.DrawLines(color, thickness, points); Assert.NotEmpty(img.ProcessorApplications); DrawPathProcessor <Rgba32> processor = Assert.IsType <DrawPathProcessor <Rgba32> >(img.ProcessorApplications[0].processor); Assert.Equal(GraphicsOptions.Default, processor.Options); ShapePath path = Assert.IsType <ShapePath>(processor.Path); SixLabors.Shapes.Path vector = Assert.IsType <SixLabors.Shapes.Path>(path.Path); LinearLineSegment segment = Assert.IsType <LinearLineSegment>(vector.LineSegments[0]); Pen <Rgba32> pen = Assert.IsType <Pen <Rgba32> >(processor.Pen); Assert.Equal(thickness, pen.Width); SolidBrush <Rgba32> brush = Assert.IsType <SolidBrush <Rgba32> >(pen.Brush); Assert.Equal(color, brush.Color); }
/// <summary> /// This will draw on the backgroup image the route and markers according to color and opacity /// </summary> /// <param name="context"></param> private void DrawRoutesOnImage(ImageCreationContext context) { context.Image.Mutate(ctx => { var routeColorIndex = 0; foreach (var route in context.DataContainer.Routes) { var points = route.Segments.SelectMany(s => s.Latlngs).Select(l => ConvertLatLngToPoint(l, context)).ToArray(); var markerPoints = route.Markers.Select(m => ConvertLatLngToPoint(m.Latlng, context)); var lineColor = _routeColors[routeColorIndex++]; routeColorIndex = routeColorIndex % _routeColors.Length; if (!string.IsNullOrEmpty(route.Color)) { lineColor = FromColorString(route.Color, route.Opacity); } if (points.Any()) { var path = new SixLabors.Shapes.Path(new LinearLineSegment(points)); ctx.Draw(Color.White, PEN_WIDTH + PEN_WIDTH_OFFSET, path); ctx.Draw(lineColor, PEN_WIDTH, path); var startCircle = new EllipsePolygon(points.First(), CIRCLE_RADIUS); ctx.Fill(Color.White, startCircle); ctx.Draw(Color.Green, CIRCLE_OUTLINE_WIDTH, startCircle); var endCircle = new EllipsePolygon(points.Last(), CIRCLE_RADIUS); ctx.Fill(Color.White, endCircle); ctx.Draw(Color.Red, CIRCLE_OUTLINE_WIDTH, endCircle); } foreach (var markerPoint in markerPoints) { var markerEllipse = new EllipsePolygon(markerPoint, CIRCLE_RADIUS); ctx.Fill(Color.White, markerEllipse); ctx.Draw(lineColor, PEN_WIDTH, markerEllipse); } } }); }
public byte[] GetValidCodeByte(string fontPath, string code) { int fSize = FontSize; int fWidth = fSize + Padding; int imageWidth = (int)(code.Length * fWidth) + 4 + Padding * 2; int imageHeight = fSize * 2 + Padding; var bb = default(byte[]); try { var dianWith = 1; //点宽度 //var xx_space = 10; //点与点之间x坐标间隔 //var yy_space = 5; //y坐标间隔 var wenZiLen = code.Length; //文字长度 //var maxX = imageWidth / wenZiLen; //每个文字最大x宽度 //var prevWenZiX = 0; //前面一个文字的x坐标 //var size = 16;//字体大小 List <SixLabors.Fonts.Font> myfonts = new List <SixLabors.Fonts.Font>(); foreach (string fontName in fonts) { //var install_Family = new FontCollection().Install($@"C:\MyWork\ziti\{fontName}.TTF"); //var install_Family = new FontCollection().Find("Arial"); myfonts.Add(new SixLabors.Fonts.Font(new FontCollection().Install($"{fontPath}{fontName}.TTF"), 50, SixLabors.Fonts.FontStyle.Bold)); } //点坐标 var listPath = new List <IPath>(); Random rand = new Random(); //给背景添加随机生成的燥点 if (this.Chaos) { //Pen pen = new Pen(ChaosColor, 0); int c = (Length) * 10 * 2; for (int i = 0; i < c; i++) { int x = rand.Next(imageWidth); int y = rand.Next(imageHeight); var position = new Vector2(x, y); var linerLine = new LinearLineSegment(position, position); var shapesPath = new SixLabors.Shapes.Path(linerLine); listPath.Add(shapesPath); } } int left = 0, top = 0, top1 = 1, top2 = 1; int n1 = (imageHeight - FontSize - Padding * 2); int n2 = n1 / 4; top1 = n2; top2 = n2 * 2; //画图 using (Image <Rgba32> image = new Image <Rgba32>(imageWidth, imageHeight)) //画布大小 { image.Mutate(x => { var imgProc = x; int cindex, findex; //随机字体和颜色的验证码字符 for (int i = 0; i < wenZiLen; i++) { cindex = rand.Next(Rgbas.Length); findex = rand.Next(Fonts.Length); var font = myfonts[findex]; var rgba = Rgbas[cindex]; //当前的要输出的字 var nowWenZi = code.Substring(i, 1); //文字坐标 var wenXY = new Vector2(); if (i % 2 == 1) { top = top2; } else { top = top1; } wenXY.X = i * fWidth; wenXY.Y = top; //prevWenZiX = Convert.ToInt32(Math.Floor(wenXY.X)) + fSize * 2; imgProc.DrawText(nowWenZi, font, rgba, wenXY); } //逐个画字 //for (int i = 0; i < wenZiLen; i++) // { // cindex = rand.Next(Rgbas.Length); // findex = rand.Next(Fonts.Length); // var font = myfonts[findex]; // var rgba = Rgbas[cindex]; // //当前的要输出的字 // var nowWenZi = content.Substring(i, 1); // //文字坐标 // var wenXY = new Vector2(); // var maxXX = prevWenZiX + (maxX - fSize); // wenXY.X = new Random().Next(prevWenZiX, maxXX); // wenXY.Y = new Random().Next(0, imageHeight - fSize); // prevWenZiX = Convert.ToInt32(Math.Floor(wenXY.X)) + fSize; // //画字 // imgProc.DrawText(nowWenZi, font, rgba, wenXY); // //imgProc.DrawText(nowWenZi, font, i % 2 > 0 ? Rgba32.HotPink : Rgba32.Red, wenXY,TextGraphicsOptions.Default); // } //画点 imgProc.BackgroundColor(Rgba32.WhiteSmoke). //画布背景 Draw( SixLabors.ImageSharp.Processing.Pens.Dot(Rgba32.HotPink, dianWith), //大小 new SixLabors.Shapes.PathCollection(listPath) //坐标集合 ).DrawLines(Rgba32.Red, //字体颜色 2, //字体大小 new SixLabors.Primitives.PointF[] { new Vector2(10, 10), new Vector2(imageWidth, imageHeight) }).DrawLines(Rgba32.Black, //字体颜色 2, //字体大小 new SixLabors.Primitives.PointF[] { new Vector2(10, imageHeight), new Vector2(imageWidth, 10) }); //两点一线坐标); }); using (MemoryStream stream = new MemoryStream()) { image.SaveAsPng(stream); bb = stream.GetBuffer(); } } } catch (Exception ex) { } return(bb); }
/// <summary> /// 画点+画字=验证码byte[] /// </summary> /// <param name="code"></param> /// <param name="vCodeNum"></param> /// <param name="xx"></param> /// <param name="yy"></param> /// <returns></returns> public static byte[] GetValidCodeByte(string fontPath, out string code, int vCodeNum = 4, int xx = 80, int yy = 25) { var bb = default(byte[]); code = RndNum(vCodeNum); try { var content = code; var dianWith = 1; //点宽度 var xx_space = 10; //点与点之间x坐标间隔 var yy_space = 5; //y坐标间隔 var wenZiLen = vCodeNum; //文字长度 var maxX = xx / wenZiLen; //每个文字最大x宽度 var prevWenZiX = 0; //前面一个文字的x坐标 var size = maxX; //字体大小 //字体 var install_Family = new FontCollection().Install(fontPath); //var install_Family = new FontCollection().Find("arial"); var font = new Font(install_Family, size); //字体 //点坐标 var listPath = new List <IPath>(); for (int i = 0; i < xx / xx_space; i++) { for (int j = 0; j < yy / yy_space; j++) { var position = new Vector2(i * xx_space, j * yy_space); var linerLine = new LinearLineSegment(position, position); var shapesPath = new SixLabors.Shapes.Path(linerLine); listPath.Add(shapesPath); } } //画图 using (Image <Rgba32> image = new Image <Rgba32>(xx, yy)) //画布大小 { image.Mutate(x => { var imgProc = x; //逐个画字 for (int i = 0; i < wenZiLen; i++) { //当前的要输出的字 var nowWenZi = content.Substring(i, 1); //文字坐标 var wenXY = new Vector2(); var maxXX = prevWenZiX + (maxX - size); wenXY.X = new Random().Next(prevWenZiX, maxXX); wenXY.Y = new Random().Next(0, yy - size); prevWenZiX = Convert.ToInt32(Math.Floor(wenXY.X)) + size; //画字 imgProc.DrawText( nowWenZi, //文字内容 font, i % 2 > 0 ? Rgba32.HotPink : Rgba32.Red, wenXY, TextGraphicsOptions.Default); } //画点 imgProc.BackgroundColor(Rgba32.WhiteSmoke). //画布背景 Draw( Pens.Dot(Rgba32.HotPink, dianWith), //大小 new PathCollection(listPath) //坐标集合 ); }); using (MemoryStream stream = new MemoryStream()) { image.SaveAsPng(stream); bb = stream.GetBuffer(); } } } catch (Exception ex) { } return(bb); }