留个档,C# 命令行,对图片进行缩放再输出

参数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 神一般的狄狄

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值