使用ImageSharp操作图片

 

导入ImageSharp库

using System;
using System.IO;
using System.Collections.Generic;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using System.Numerics;
using SixLabors.ImageSharp.PixelFormats;
namespace ImageSharpLearn
{
    class Program
    {
        static string imagePath = "E:\\Harry\\C# Projects\\ImageSharpLearn\\ImageSharpLearn\\Textures";
        static void Main(string[] args)
        {
            Image back = ProcessTexture("Test.png");
            Image addImg = Image.Load(Path.Combine(imagePath, "Add.png"));
            MergeImg(addImg, back, new Point(100, 0));
            Console.ReadLine();
        }

        static void ResizeTestPng()
        {

            Image img = Image.Load(Path.Combine(imagePath, "GrassUV02.png"));
            img.Mutate(o =>
            {
                o.Resize(256, 256);
            });
            img.Save(Path.GetDirectoryName(imagePath) + "/Test.png");
            Console.WriteLine("ResizeTestPng");
        }

        /// <summary>
        /// 切割图片
        /// </summary>
        static Image ProcessTexture(string textName)
        {
            Image<Rgba32> img = Image.Load<Rgba32>(Path.Combine(imagePath, textName));
            //六角格的位置
            Vector2 leftPos = new Vector2(0, 0);
            Vector2 topLeft = new Vector2(2.5f, 4);
            Vector2 topRight = new Vector2(7.5f, 4f);
            Vector2 rightPos = new Vector2(10, 0);
            Vector2 bottomLeft = new Vector2(2.5f, -4);
            Vector2 bottomRight = new Vector2(7.5f, -4f);
            Vector2 centerPos = new Vector2(5, 0);
            float faceSideDis = topLeft.Y - bottomLeft.Y;
            float faceVertexDis = rightPos.X - leftPos.X;
            float longDis = Math.Max(faceSideDis, faceVertexDis);
            float halfLongDis = longDis / 2;
            //假设图片格式为Odd_q正六角格,对角距离就是长宽,其他部分重复填充
            if (faceSideDis > faceVertexDis)
            {
                float expandRatio = faceSideDis / faceVertexDis;
                img.Mutate(x =>
                {
                    x.Resize((int)(img.Width * expandRatio), (int)(img.Height * expandRatio));
                }
                );
            }
            int widht = img.Width;
            int height = img.Height;
            float leftX = centerPos.X - halfLongDis;
            float topY = centerPos.Y + halfLongDis;
            //计算六角格的位置在图中的比例
            Vector2 leftPercentPos = new Vector2((leftPos.X - leftX) / longDis, (leftPos.Y - topY) / longDis);
            Vector2 topLeftPercentPos = new Vector2((topLeft.X - leftX) / longDis, (topLeft.Y - topY) / longDis);
            Vector2 topRightPercentPos = new Vector2((topRight.X - leftX) / longDis, (topRight.Y - topY) / longDis);
            Vector2 rightPercentPos = new Vector2((rightPos.X - leftX) / longDis, (rightPos.Y - topY) / longDis);
            Vector2 bottomLeftPercentPos = new Vector2((bottomLeft.X - leftX) / longDis, (bottomLeft.Y - topY) / longDis);
            Vector2 bottomRightPercentPos = new Vector2((bottomRight.X - leftX) / longDis, (bottomRight.Y - topY) / longDis);

            Vector2 leftPixPos = new Vector2((int)Math.Round(leftPercentPos.X * widht), (int)Math.Round(leftPercentPos.Y * height));
            Vector2 topLeftPixPos = new Vector2((int)Math.Round(topLeftPercentPos.X * widht), (int)Math.Round(topLeftPercentPos.Y * height));
            Vector2 topRightPixPos = new Vector2((int)Math.Round(topRightPercentPos.X * widht), (int)Math.Round(topRightPercentPos.Y * height));
            Vector2 rightPixPos = new Vector2((int)Math.Round(rightPercentPos.X * widht), (int)Math.Round(rightPercentPos.Y * height));
            Vector2 bottomLeftPixPos = new Vector2((int)Math.Round(bottomLeftPercentPos.X * widht), (int)Math.Round(bottomLeftPercentPos.Y * height));
            Vector2 bottomRightPixPos = new Vector2((int)Math.Round(bottomRightPercentPos.X * widht), (int)Math.Round(bottomRightPercentPos.Y * height));
            List<Vector2> polyPoints = new List<Vector2>() { leftPixPos, topLeftPixPos, topRightPixPos, rightPixPos, bottomRightPixPos, bottomLeftPixPos };
            for (int i = 0; i < widht; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    if (!CheckPointInPolygon(new Vector2(i, -j), polyPoints))
                    {
                        img[i, j] = Rgba32.ParseHex("00000000");
                    }
                }
            }
            int newWidth = (int)(rightPixPos.X - leftPixPos.X) + 1;
            newWidth = Math.Min(widht, newWidth);
            int newHeight = (int)(bottomLeftPixPos.Y - topLeftPixPos.Y) + 1;
            newHeight = Math.Min(newHeight, height);
            img.Mutate(x => x.Crop(new Rectangle((int)leftPixPos.X, -(int)topLeftPixPos.Y, Math.Abs(newWidth), Math.Abs(newHeight))));
            img.Save(Path.Combine(imagePath, textName + "_Hex.png"));
            Console.WriteLine("ProcessTexture");
            return img;
        }

        static void MergeImg(Image source, Image back, Point start)
        {
            back.Mutate(x =>
            {
                x.DrawImage(source, start, 1);
            });
            back.Save(Path.Combine(imagePath, "Hex_Merge.png"));
        }

        static bool CheckPointInPolygon(Vector2 p, List<Vector2> polyPoints)
        {
            var j = polyPoints.Count - 1;
            var inside = false;
            for (int i = 0; i < polyPoints.Count; j = i++)
            {
                var pi = polyPoints[i];
                var pj = polyPoints[j];
                if (((pi.Y <= p.Y && p.Y < pj.Y) || (pj.Y <= p.Y && p.Y < pi.Y)) &&
                    (p.X < (pj.X - pi.X) * (p.Y - pi.Y) / (pj.Y - pi.Y) + pi.X))
                    inside = !inside;
            }
            return inside;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值