c# 通过现在文件夹,获取下面所有的照片,并对其进行统一尺寸裁剪
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection.Emit;
namespace _11_裁剪照片
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){ArrayList list = new ArrayList();string path = textBox1.Text;string path_bc = textBox2.Text;DirectoryInfo folder = new DirectoryInfo(path);FileSystemInfo fileinfo1 = folder as FileSystemInfo;list = selecte_jpg(fileinfo1);progressBar1.Maximum = list.Count;progressBar1.Minimum = 0;int i = 0;foreach (string path_jpg in list){string pathname = path_bc +'\\' + System.IO.Path.GetFileName(path_jpg);cj_jpg(path_jpg, pathname);setPos(i);i++;}setPos(i);}private void cj_jpg(string path_y, string path_bc) {Image originImage = Image.FromFile(path_y);//创建矩形对象表示原图上裁剪的矩形区域,这里相当于划定原图上坐标为(10, 10)处,50x50大小的矩形区域为裁剪区域Rectangle cropRegion = new Rectangle(0, 0, 900, 768);//创建空白画布,大小为裁剪区域大小Bitmap result = new Bitmap(cropRegion.Width, cropRegion.Height);//创建Graphics对象,并指定要在result(目标图片画布)上绘制图像Graphics graphics = Graphics.FromImage(result);//使用Graphics对象把原图指定区域图像裁剪下来并填充进刚刚创建的空白画布graphics.DrawImage(originImage, new Rectangle(0, 0, cropRegion.Width, cropRegion.Height), cropRegion, GraphicsUnit.Pixel);//这个时候裁剪区域图片就被填充进result对象中去了,可以对其进行保存result.Save(path_bc, ImageFormat.Png);}private static ArrayList selecte_jpg(FileSystemInfo info){DirectoryInfo dir = info as DirectoryInfo;ArrayList listPics = new ArrayList();FileSystemInfo[] files = dir.GetFileSystemInfos();for (int i = 0; i < files.Length; i++){FileInfo file = files[i] as FileInfo;//是文件if (file != null){string extension = Path.GetExtension(file.Name);if (extension.ToUpper() == ".PNG")listPics.Add(file.FullName);}else//对于子目录,进行递归调用selecte_jpg(files[i]);}return listPics;}private void button2_Click(object sender, EventArgs e){FolderBrowserDialog dialog = new FolderBrowserDialog();dialog.Description = "请选择文件路径";if (dialog.ShowDialog() == DialogResult.OK){string savePath = dialog.SelectedPath;textBox1.Text = savePath;}}private void button3_Click(object sender, EventArgs e){string Path = "";FolderBrowserDialog folder = new FolderBrowserDialog();folder.Description = "选择文件所在文件夹目录"; //提示的文字if (folder.ShowDialog() == DialogResult.OK){Path = folder.SelectedPath;textBox2.Text = Path;}}private void progressBar1_Click(object sender, EventArgs e){}private void setPos(int value) //设置进度条当前进度值{if (value < progressBar1.Maximum + 1) //如果值有效{progressBar1.Value = value; //设置进度值}Application.DoEvents();//重点,必须加上,否则父子窗体都假死}
}
}