C#分离Alpha通道图

作者:追风剑情 发布于:2018-11-23 18:27 分类:Unity3d

一、工程
111.png
示例代码
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;

namespace SplitAlpha
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length <= 0) {
                Console.WriteLine("missing param");
                return;
            }
            string filename = args[0];
            Bitmap bmp = new Bitmap(filename);
            if (bmp == null) {
                Console.WriteLine("load failed: " + filename);
                return;
            }
            Bitmap bmp_rgb = bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height), PixelFormat.Format24bppRgb);
            Bitmap bmp_alpha = bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height), PixelFormat.Format24bppRgb);
            Color c, new_c;
            for (int x = 0; x < bmp.Width; x++)
            {
                for (int y = 0; y < bmp.Height; y++)
                {
                    c = bmp.GetPixel(x, y);
                    new_c = Color.FromArgb(1, c.A, c.A, c.A);
                    bmp_alpha.SetPixel(x, y, new_c);
                }
            }

            string file_path = filename;
            string tmp_path = Path.GetDirectoryName(file_path);
            string tmp_name = Path.GetFileNameWithoutExtension(file_path);
            string rgb_path = string.Format("{0}/{1}_RGB.png", tmp_path, tmp_name);
            string alpha_path = string.Format("{0}/{1}_alpha.png", tmp_path, tmp_name);

            bmp_rgb.Save(rgb_path, ImageFormat.Png);
            bmp_alpha.Save(alpha_path, ImageFormat.Png);

            bmp.Dispose();
            bmp_rgb.Dispose();
            bmp_alpha.Dispose();
        }
    }
}

通过批处理调用
echo off
set png_file=png文件路径
::分离Alpha通道
SplitAlpha.exe %png_file%
@pause

测试效果

原图
22.png
RGB图
333.png
Alpha通道图
444.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号