c#按图片尺寸整理图片

static void Main(string[] args)
        {
            string path = "C:/Users/liqi/Desktop/ad";
            var files = Directory.GetFiles(path);
            string width = string.Empty; //图片的高
            string height = string.Empty;//图片的宽
            string dir = string.Empty;
            foreach (var file in files)
            {
                if (file.Contains(".jpg") || file.Contains(".gif") || file.Contains(".png"))
                {
                    var fileinfo = new FileInfo(file);
                    using (var img = Image.FromFile(file))
                    {
                        //取得图片的宽和高
                        width = img.Width.ToString();
                        height = img.Height.ToString();
                    }
                    dir = Path.Combine(fileinfo.DirectoryName, width + "x" + height);
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    fileinfo.MoveTo(Path.Combine(dir, width + "x" + height + "_" + GetRandName() + fileinfo.Extension));
                    Thread.Sleep(1);
                }
            }
        }

        private static string GetRandName()
        {
            var time = DateTime.Now;
            return time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
        }

你可能感兴趣的:(c#按图片尺寸整理图片)