参数1 = 源图片
参数2 = 生成目标文件
参数3 = 宽
参数4 = 高
在做unity工具的时候,发现如果直接做在引擎里面,api有限,不太好处理。用比较接近底层的实现方法去操作会比较好。
所以用C#整了一个命令行工具,方便批量处理图片的分辨率修改。
namespace TextureScale
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
args = new string[]
{
"E:/copywork/1.png",
"E:/copywork/2.png",
"64",
"64"
};
}
if (args.Length != 4)
{
Console.WriteLine("Length Error, Need 4 Param: FromPath SavePath Width Height");
return;
}
string strFromPath = args[0];
string strSavePath = args[1];
int nWidth = 0;
int.TryParse(args[2], out nWidth);
int nHeight = 0;
int.TryParse(args[3], out nHeight);
if (nWidth == 0 || nHeight == 0)
{
Console.WriteLine("Width or Height can not be Zero");
return;
}
Bitmap bm = Bitmap.FromFile(strFromPath) as Bitmap;
bm = ZoomImage(bm, nWidth, nHeight);
bm.Save(strSavePath);
}
private static Bitmap ZoomImage(Bitmap bitmap, int destHeight, int destWidth)
{
try
{
System.Drawing.Image sourImage = bitmap;
int width = 0, height = 0;
//按比例缩放
int sourWidth = sourImage.Width;
int sourHeight = sourImage.Height;
if ((sourWidth * destHeight) > (sourHeight * destWidth))
{
width = destWidth;
height = (destWidth * sourHeight) / sourWidth;
}
else
{
height = destHeight;
width = (sourWidth * destHeight) / sourHeight;
}
Bitmap destBitmap = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage(destBitmap);
g.Clear(Color.Transparent);
//设置画布的描绘质量
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(sourImage, new Rectangle(0, 0, destWidth, destHeight), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel);
g.Dispose();
//设置压缩质量
System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
sourImage.Dispose();
return destBitmap;
}
catch
{
return bitmap;
}
}
}
}
程序学无止尽。
欢迎大家沟通,有啥不明确的,或者不对的,也可以和我私聊
我的QQ 334524067 神一般的狄狄