当前位置:   article > 正文

.Net Core 使用 ImageSharp 画图形_sixlabors.imagesharp

sixlabors.imagesharp

首先在项目中NuGet添加程序包:SixLabors.ImageSharp 和 SixLabors.ImageSharp.Drawing

接下来就可以愉快的玩耍啦


画贝塞尔曲线:

  1. using (var image = new Image<Rgba32>(500, 500))
  2. {
  3. //贝塞尔曲线
  4. image.Mutate(
  5. x => x.BackgroundColor(Rgba32.Blue).DrawBeziers(
  6. Rgba32.HotPink,
  7. 10,
  8. new SixLabors.Primitives.PointF[]
  9. {
  10. new Vector2(10, 400), new Vector2(30, 10), new Vector2(240, 30), new Vector2(300, 400)
  11. }));
  12. image.Save("./images/test.png");
  13. }

BackgroundColor(Rgba32.Blue)定义画布背景色;

DrawBeziers的参数:Rgba32.HotPink定义曲线颜色,10定义曲线粗细, new SixLabors.Primitives.PointF[]{};定义曲线一系列坐标。

以上代码画出来的图形是这样的:

 


画多条曲线(折线+曲线):

  1. using (var image = new Image<Rgba32>(500, 500))
  2. {
  3. //曲线
  4. var linerSegemnt = new LinearLineSegment(
  5. new Vector2(10, 10),
  6. new Vector2(200, 150),
  7. new Vector2(50, 300));
  8. var bazierSegment = new CubicBezierLineSegment(
  9. new Vector2(50, 300),
  10. new Vector2(500, 500),
  11. new Vector2(60, 10),
  12. new Vector2(10, 400));
  13. var p = new Path(linerSegemnt, bazierSegment);
  14. image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Draw(Rgba32.HotPink, 5, p));
  15. image.Save("./images/test.png");
  16. }

效果:


画三角形:

  1. using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
  2. {
  3. image.Mutate(
  4. x => x.BackgroundColor(Rgba32.Blue).DrawPolygon(
  5. Rgba32.HotPink,
  6. 5,
  7. new Vector2(10, 10),
  8. new Vector2(200, 150),
  9. new Vector2(50, 300)));
  10. image.Save("./images/test.png");
  11. }

可以对一个图形使用Clip()方法在其内部切割一个图形:

  1. var simplePath = new Polygon(new LinearLineSegment(
  2. new Vector2(10, 10),
  3. new Vector2(200, 150),
  4. new Vector2(50, 300)));
  5. var hole1 = new Polygon(new LinearLineSegment(
  6. new Vector2(37, 85),
  7. new Vector2(93, 85),
  8. new Vector2(65, 137)));
  9. using (var image = new Image<Rgba32>(500, 500))
  10. {
  11. image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Draw(Rgba32.HotPink, 5, simplePath.Clip(hole1)));
  12. image.Save("./images/test.png");
  13. }


矩形:

  1. using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
  2. {
  3. image.Mutate(
  4. x => x.BackgroundColor(Rgba32.Blue).DrawPolygon(
  5. Rgba32.HotPink,
  6. 5,
  7. new Vector2(0, 0),
  8. new Vector2(300, 0),
  9. new Vector2(300, 300),
  10. new Vector2(0, 300)));
  11. image.Save("./images/test.png");
  12. }

可以使用Fill()方法给图形填充颜色,使用Rotate()方法旋转图形:

  1. var simplePath = new Polygon(new LinearLineSegment(
  2. new Vector2(0, 0),
  3. new Vector2(300, 0),
  4. new Vector2(300, 300),
  5. new Vector2(0, 300)
  6. ));
  7. var hole1 = new Polygon(new LinearLineSegment(
  8. new Vector2(37, 85),
  9. new Vector2(93, 85),
  10. new Vector2(65, 137)));
  11. IPath clipped = simplePath.Clip(hole1);
  12. using (var image = new Image<Rgba32>(500, 500))
  13. {
  14. float rotate = 90;
  15. image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Fill(Rgba32.HotPink, clipped).Rotate(rotate));
  16. image.Save("./images/test.png");
  17. }


画圆:

  1. using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
  2. {
  3. int scaleX = (image.Width / 100);
  4. int scaleY = (image.Height / 100);
  5. var c = NamedColors<Rgba32>.Red.ToVector4();
  6. var pixel = default(Rgba32);
  7. pixel.PackFromVector4(c);
  8. image.Mutate(
  9. x => x.Fill(
  10. new GraphicsOptions(true) { },
  11. pixel,
  12. new SixLabors.Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY))
  13. );
  14. image.Save("./images/test.png");
  15. }

可以改变Vector4对象的W属性对图形进行透明度设置:

  1. using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
  2. {
  3. int scaleX = (image.Width / 100);
  4. int scaleY = (image.Height / 100);
  5. image.Mutate(
  6. x => x.Fill(
  7. NamedColors<Rgba32>.DarkBlue,
  8. new Rectangle(0 * scaleX, 40, 100 * scaleX, 20 * scaleY)));
  9. image.Mutate(
  10. x => x.Fill(
  11. new GraphicsOptions(true) { },
  12. NamedColors<Rgba32>.HotPink,
  13. new Rectangle(20 * scaleX, 0, 30 * scaleX, 100 * scaleY)));
  14. var c = NamedColors<Rgba32>.Red.ToVector4();
  15. c.W *= 0.5f;
  16. var pixel = default(Rgba32);
  17. pixel.PackFromVector4(c);
  18. image.Mutate(
  19. x => x.Fill(
  20. new GraphicsOptions(true) { },
  21. pixel,
  22. new SixLabors.Shapes.EllipsePolygon(40 * scaleX, 50 * scaleY, 50 * scaleX, 50 * scaleY))
  23. );
  24. image.Save("./images/test.png");
  25. }


画五角星:

  1. var simplePath = new Polygon(new LinearLineSegment(
  2. new Vector2(250f, 0f),
  3. new Vector2(327.3f, 156.5f),
  4. new Vector2(500f, 181.6f),
  5. new Vector2(375f, 303.5f),
  6. new Vector2(404.5f, 475.5f),
  7. new Vector2(250f, 394.3f),
  8. new Vector2(95.5f, 475.5f),
  9. new Vector2(125f, 303.5f),
  10. new Vector2(0f, 181.6f),
  11. new Vector2(172.7f, 156.5f)
  12. ));
  13. using (var image = new Image<Rgba32>(510, 500))
  14. {
  15. image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Fill(Rgba32.HotPink, simplePath));
  16. image.Save("./images/test.png");
  17. }


画正八边形:

  1. var simplePath = new Polygon(new LinearLineSegment(
  2. new Vector2(146.4f, 500f),
  3. new Vector2(0f, 353.6f),
  4. new Vector2(0f, 146.4f),
  5. new Vector2(146.4f, 0f),
  6. new Vector2(353.6f, 0f),
  7. new Vector2(500f, 146.4f),
  8. new Vector2(500f, 353.6f),
  9. new Vector2(353.6f, 500f)
  10. ));
  11. using (var image = new Image<Rgba32>(500, 500))
  12. {
  13. image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Fill(Rgba32.HotPink, simplePath));
  14. image.Save("./images/test.png");
  15. }


大概就这些吧。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号