
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
测试效果