当前位置:   article > 正文

使用ImageSharp操作图片_c# sixlabors.imagesharp 剪切图片

c# sixlabors.imagesharp 剪切图片

 

导入ImageSharp库

  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using SixLabors.ImageSharp;
  5. using SixLabors.ImageSharp.Processing;
  6. using System.Numerics;
  7. using SixLabors.ImageSharp.PixelFormats;
  8. namespace ImageSharpLearn
  9. {
  10. class Program
  11. {
  12. static string imagePath = "E:\\Harry\\C# Projects\\ImageSharpLearn\\ImageSharpLearn\\Textures";
  13. static void Main(string[] args)
  14. {
  15. Image back = ProcessTexture("Test.png");
  16. Image addImg = Image.Load(Path.Combine(imagePath, "Add.png"));
  17. MergeImg(addImg, back, new Point(100, 0));
  18. Console.ReadLine();
  19. }
  20. static void ResizeTestPng()
  21. {
  22. Image img = Image.Load(Path.Combine(imagePath, "GrassUV02.png"));
  23. img.Mutate(o =>
  24. {
  25. o.Resize(256, 256);
  26. });
  27. img.Save(Path.GetDirectoryName(imagePath) + "/Test.png");
  28. Console.WriteLine("ResizeTestPng");
  29. }
  30. /// <summary>
  31. /// 切割图片
  32. /// </summary>
  33. static Image ProcessTexture(string textName)
  34. {
  35. Image<Rgba32> img = Image.Load<Rgba32>(Path.Combine(imagePath, textName));
  36. //六角格的位置
  37. Vector2 leftPos = new Vector2(0, 0);
  38. Vector2 topLeft = new Vector2(2.5f, 4);
  39. Vector2 topRight = new Vector2(7.5f, 4f);
  40. Vector2 rightPos = new Vector2(10, 0);
  41. Vector2 bottomLeft = new Vector2(2.5f, -4);
  42. Vector2 bottomRight = new Vector2(7.5f, -4f);
  43. Vector2 centerPos = new Vector2(5, 0);
  44. float faceSideDis = topLeft.Y - bottomLeft.Y;
  45. float faceVertexDis = rightPos.X - leftPos.X;
  46. float longDis = Math.Max(faceSideDis, faceVertexDis);
  47. float halfLongDis = longDis / 2;
  48. //假设图片格式为Odd_q正六角格,对角距离就是长宽,其他部分重复填充
  49. if (faceSideDis > faceVertexDis)
  50. {
  51. float expandRatio = faceSideDis / faceVertexDis;
  52. img.Mutate(x =>
  53. {
  54. x.Resize((int)(img.Width * expandRatio), (int)(img.Height * expandRatio));
  55. }
  56. );
  57. }
  58. int widht = img.Width;
  59. int height = img.Height;
  60. float leftX = centerPos.X - halfLongDis;
  61. float topY = centerPos.Y + halfLongDis;
  62. //计算六角格的位置在图中的比例
  63. Vector2 leftPercentPos = new Vector2((leftPos.X - leftX) / longDis, (leftPos.Y - topY) / longDis);
  64. Vector2 topLeftPercentPos = new Vector2((topLeft.X - leftX) / longDis, (topLeft.Y - topY) / longDis);
  65. Vector2 topRightPercentPos = new Vector2((topRight.X - leftX) / longDis, (topRight.Y - topY) / longDis);
  66. Vector2 rightPercentPos = new Vector2((rightPos.X - leftX) / longDis, (rightPos.Y - topY) / longDis);
  67. Vector2 bottomLeftPercentPos = new Vector2((bottomLeft.X - leftX) / longDis, (bottomLeft.Y - topY) / longDis);
  68. Vector2 bottomRightPercentPos = new Vector2((bottomRight.X - leftX) / longDis, (bottomRight.Y - topY) / longDis);
  69. Vector2 leftPixPos = new Vector2((int)Math.Round(leftPercentPos.X * widht), (int)Math.Round(leftPercentPos.Y * height));
  70. Vector2 topLeftPixPos = new Vector2((int)Math.Round(topLeftPercentPos.X * widht), (int)Math.Round(topLeftPercentPos.Y * height));
  71. Vector2 topRightPixPos = new Vector2((int)Math.Round(topRightPercentPos.X * widht), (int)Math.Round(topRightPercentPos.Y * height));
  72. Vector2 rightPixPos = new Vector2((int)Math.Round(rightPercentPos.X * widht), (int)Math.Round(rightPercentPos.Y * height));
  73. Vector2 bottomLeftPixPos = new Vector2((int)Math.Round(bottomLeftPercentPos.X * widht), (int)Math.Round(bottomLeftPercentPos.Y * height));
  74. Vector2 bottomRightPixPos = new Vector2((int)Math.Round(bottomRightPercentPos.X * widht), (int)Math.Round(bottomRightPercentPos.Y * height));
  75. List<Vector2> polyPoints = new List<Vector2>() { leftPixPos, topLeftPixPos, topRightPixPos, rightPixPos, bottomRightPixPos, bottomLeftPixPos };
  76. for (int i = 0; i < widht; i++)
  77. {
  78. for (int j = 0; j < height; j++)
  79. {
  80. if (!CheckPointInPolygon(new Vector2(i, -j), polyPoints))
  81. {
  82. img[i, j] = Rgba32.ParseHex("00000000");
  83. }
  84. }
  85. }
  86. int newWidth = (int)(rightPixPos.X - leftPixPos.X) + 1;
  87. newWidth = Math.Min(widht, newWidth);
  88. int newHeight = (int)(bottomLeftPixPos.Y - topLeftPixPos.Y) + 1;
  89. newHeight = Math.Min(newHeight, height);
  90. img.Mutate(x => x.Crop(new Rectangle((int)leftPixPos.X, -(int)topLeftPixPos.Y, Math.Abs(newWidth), Math.Abs(newHeight))));
  91. img.Save(Path.Combine(imagePath, textName + "_Hex.png"));
  92. Console.WriteLine("ProcessTexture");
  93. return img;
  94. }
  95. static void MergeImg(Image source, Image back, Point start)
  96. {
  97. back.Mutate(x =>
  98. {
  99. x.DrawImage(source, start, 1);
  100. });
  101. back.Save(Path.Combine(imagePath, "Hex_Merge.png"));
  102. }
  103. static bool CheckPointInPolygon(Vector2 p, List<Vector2> polyPoints)
  104. {
  105. var j = polyPoints.Count - 1;
  106. var inside = false;
  107. for (int i = 0; i < polyPoints.Count; j = i++)
  108. {
  109. var pi = polyPoints[i];
  110. var pj = polyPoints[j];
  111. if (((pi.Y <= p.Y && p.Y < pj.Y) || (pj.Y <= p.Y && p.Y < pi.Y)) &&
  112. (p.X < (pj.X - pi.X) * (p.Y - pi.Y) / (pj.Y - pi.Y) + pi.X))
  113. inside = !inside;
  114. }
  115. return inside;
  116. }
  117. }
  118. }

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

闽ICP备14008679号