【C#】制作图集

如题目,用好几个图片拼在一个大图里,博主是用于Unity游戏开发使用的,话不多说,上代码!

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;

namespace EffectsPackTool.Common
{
    /// <summary>
    /// 图集处理共通方法
    /// </summary>
    public static class SpriteSheet
    {
        /// <summary>
        /// 创建图集
        /// </summary>
        /// <param name="imagePaths">图片地址集</param>
        /// <param name="outputPath">输出路径</param>
        /// <param name="maxWidth">画布最大宽度</param>
        /// <param name="maxHeight">画布最大高度</param>
        /// <param name="imageMaxHeight">图片最大高度</param>
        public static bool CreateSpriteSheet(IEnumerable<string> imagePaths, string outputPath, int maxWidth, int maxHeight, int imageMaxHeight)
        {
            try
            {
                using (Bitmap spriteSheet = new Bitmap(maxWidth, maxHeight))
                {
                    using (Graphics g = Graphics.FromImage(spriteSheet))
                    {
                        g.Clear(Color.Transparent); // 如果需要透明背景可以设置
                        int currentHeight = 0;
                        int currentWidth = 0;

                        foreach (string imagePath in imagePaths)
                        {
                            using (Image image = Image.FromFile(imagePath))
                            {
                                // 检查是否超出最大宽度,如果是,则移至下一行
                                if (image.Width + currentWidth > maxWidth)
                                {
                                    currentHeight += imageMaxHeight;
                                    currentWidth = 0;
                                }

                                // 绘制图像到sprite sheet
                                g.DrawImage(image, new Point(currentWidth, currentHeight));

                                // 更新当前宽度
                                currentWidth += image.Width;
                            }
                        }
                    }

                    // 保存sprite sheet到指定路径
                    spriteSheet.Save(outputPath, ImageFormat.Png);
                }

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}

没什么好说的 拿去直接用就行

完结!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值