赞
踩
首先在项目中NuGet添加程序包:SixLabors.ImageSharp 和 SixLabors.ImageSharp.Drawing
接下来就可以愉快的玩耍啦
画贝塞尔曲线:
- using (var image = new Image<Rgba32>(500, 500))
- {
- //贝塞尔曲线
- image.Mutate(
- x => x.BackgroundColor(Rgba32.Blue).DrawBeziers(
- Rgba32.HotPink,
- 10,
- new SixLabors.Primitives.PointF[]
- {
- new Vector2(10, 400), new Vector2(30, 10), new Vector2(240, 30), new Vector2(300, 400)
- }));
-
- image.Save("./images/test.png");
- }
BackgroundColor(Rgba32.Blue)定义画布背景色;
DrawBeziers的参数:Rgba32.HotPink定义曲线颜色,10定义曲线粗细, new SixLabors.Primitives.PointF[]{};定义曲线一系列坐标。
以上代码画出来的图形是这样的:
画多条曲线(折线+曲线):
- using (var image = new Image<Rgba32>(500, 500))
- {
- //曲线
- var linerSegemnt = new LinearLineSegment(
- new Vector2(10, 10),
- new Vector2(200, 150),
- new Vector2(50, 300));
- var bazierSegment = new CubicBezierLineSegment(
- new Vector2(50, 300),
- new Vector2(500, 500),
- new Vector2(60, 10),
- new Vector2(10, 400));
- var p = new Path(linerSegemnt, bazierSegment);
- image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Draw(Rgba32.HotPink, 5, p));
-
- image.Save("./images/test.png");
- }
效果:
画三角形:
- using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
- {
- image.Mutate(
- x => x.BackgroundColor(Rgba32.Blue).DrawPolygon(
- Rgba32.HotPink,
- 5,
- new Vector2(10, 10),
- new Vector2(200, 150),
- new Vector2(50, 300)));
- image.Save("./images/test.png");
- }
可以对一个图形使用Clip()方法在其内部切割一个图形:
- var simplePath = new Polygon(new LinearLineSegment(
- new Vector2(10, 10),
- new Vector2(200, 150),
- new Vector2(50, 300)));
-
- var hole1 = new Polygon(new LinearLineSegment(
- new Vector2(37, 85),
- new Vector2(93, 85),
- new Vector2(65, 137)));
-
- using (var image = new Image<Rgba32>(500, 500))
- {
- image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Draw(Rgba32.HotPink, 5, simplePath.Clip(hole1)));
- image.Save("./images/test.png");
-
- }
矩形:
- using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
- {
- image.Mutate(
- x => x.BackgroundColor(Rgba32.Blue).DrawPolygon(
- Rgba32.HotPink,
- 5,
- new Vector2(0, 0),
- new Vector2(300, 0),
- new Vector2(300, 300),
- new Vector2(0, 300)));
- image.Save("./images/test.png");
- }
可以使用Fill()方法给图形填充颜色,使用Rotate()方法旋转图形:
- var simplePath = new Polygon(new LinearLineSegment(
- new Vector2(0, 0),
- new Vector2(300, 0),
- new Vector2(300, 300),
- new Vector2(0, 300)
- ));
-
- var hole1 = new Polygon(new LinearLineSegment(
- new Vector2(37, 85),
- new Vector2(93, 85),
- new Vector2(65, 137)));
- IPath clipped = simplePath.Clip(hole1);
- using (var image = new Image<Rgba32>(500, 500))
- {
- float rotate = 90;
- image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Fill(Rgba32.HotPink, clipped).Rotate(rotate));
- image.Save("./images/test.png");
- }
画圆:
- using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
- {
- int scaleX = (image.Width / 100);
- int scaleY = (image.Height / 100);
- var c = NamedColors<Rgba32>.Red.ToVector4();
- var pixel = default(Rgba32);
- pixel.PackFromVector4(c);
- image.Mutate(
- x => x.Fill(
- new GraphicsOptions(true) { },
- pixel,
- new SixLabors.Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY))
- );
-
-
- image.Save("./images/test.png");
- }
可以改变Vector4对象的W属性对图形进行透明度设置:
- using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
- {
- int scaleX = (image.Width / 100);
- int scaleY = (image.Height / 100);
- image.Mutate(
- x => x.Fill(
- NamedColors<Rgba32>.DarkBlue,
- new Rectangle(0 * scaleX, 40, 100 * scaleX, 20 * scaleY)));
- image.Mutate(
- x => x.Fill(
- new GraphicsOptions(true) { },
- NamedColors<Rgba32>.HotPink,
- new Rectangle(20 * scaleX, 0, 30 * scaleX, 100 * scaleY)));
-
- var c = NamedColors<Rgba32>.Red.ToVector4();
- c.W *= 0.5f;
- var pixel = default(Rgba32);
- pixel.PackFromVector4(c);
- image.Mutate(
- x => x.Fill(
- new GraphicsOptions(true) { },
- pixel,
- new SixLabors.Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY))
- );
-
- image.Save("./images/test.png");
- }
画五角星:
- var simplePath = new Polygon(new LinearLineSegment(
- new Vector2(250f, 0f),
- new Vector2(327.3f, 156.5f),
- new Vector2(500f, 181.6f),
- new Vector2(375f, 303.5f),
- new Vector2(404.5f, 475.5f),
- new Vector2(250f, 394.3f),
- new Vector2(95.5f, 475.5f),
- new Vector2(125f, 303.5f),
- new Vector2(0f, 181.6f),
- new Vector2(172.7f, 156.5f)
- ));
-
- using (var image = new Image<Rgba32>(510, 500))
- {
- image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Fill(Rgba32.HotPink, simplePath));
- image.Save("./images/test.png");
- }
画正八边形:
- var simplePath = new Polygon(new LinearLineSegment(
- new Vector2(146.4f, 500f),
- new Vector2(0f, 353.6f),
- new Vector2(0f, 146.4f),
- new Vector2(146.4f, 0f),
- new Vector2(353.6f, 0f),
- new Vector2(500f, 146.4f),
- new Vector2(500f, 353.6f),
- new Vector2(353.6f, 500f)
- ));
-
- using (var image = new Image<Rgba32>(500, 500))
- {
- image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Fill(Rgba32.HotPink, simplePath));
- image.Save("./images/test.png");
- }
大概就这些吧。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。