class Program
{
static double size = 2.443438914027149;
static void Main(string[] args)
{
if (!System.IO.Directory.Exists("Img")){
System.IO.Directory.CreateDirectory("Img");
Console.WriteLine("已创建Img目录,请把需要处理的图片放到该目录下");
Console.ReadKey(true);
return;
}
if (!System.IO.Directory.Exists("NewImg")){
System.IO.Directory.CreateDirectory("NewImg");
}
GetPath("Img");
Console.ReadKey(true);
}
//读取文件夹中所有文件
static public void GetPath(string dir) {
foreach (string d in Directory.GetFileSystemEntries(dir)) {
if (File.Exists(d)) {
FileInfo fi = new FileInfo(d);
if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1) {
fi.Attributes = FileAttributes.Normal;
}
ChangeImageSize(dir);
break;
}else {
DirectoryInfo d1 = new DirectoryInfo(d);
GetPath(dir +"/" +d1.Name);
}
}
}
static public void ChangeImageSize(string dir) {
string[] fileStrs = System.IO.Directory.GetFiles(dir);
foreach (string s in fileStrs) {
System.IO.FileInfo info = new System.IO.FileInfo(s);
if (info.Extension == ".png" || info.Extension == ".jpg") {
Image img = GetFile(s);
Bitmap bit = GetNewSizeBitmap(img, size);
SaveImage(bit, info.Name, info.Extension,dir);
}
}
}
static public Image GetFile(string path)
{
FileStream stream = File.OpenRead(path);
int fileLength = 0;
fileLength = (int)stream.Length;
Byte[] image = new Byte[fileLength];
stream.Read(image, 0, fileLength);
System.Drawing.Image result = System.Drawing.Image.FromStream(stream);
stream.Close();
return result;
}
static public Bitmap GetNewSizeBitmap(Image img , double size){
int newWidth = Convert.ToInt32(img.Width / size);
int newHeight = Convert.ToInt32(img.Height / size);
Size s = new Size(newWidth, newHeight);
Bitmap newBit = new Bitmap(img, s);
return newBit;
}
static public void SaveImage(Bitmap bit, string name, string ext, string dir)
{
if (!System.IO.Directory.Exists("NewImg\\" + dir)) {
System.IO.Directory.CreateDirectory("NewImg\\" + dir);
}
bit.Save(@"NewImg\\" + dir + "\\" + name);
bit.Dispose();
Console.WriteLine("已处理:" + name);
}
}
null