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();
}文章来源:https://www.toymoban.com/news/detail-425908.html
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();//重点,必须加上,否则父子窗体都假死
}
}
}文章来源地址https://www.toymoban.com/news/detail-425908.html
到了这里,关于c# 通过现在文件夹,获取下面所有的照片,并对其进行统一尺寸裁剪的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!