c# 通过现在文件夹,获取下面所有的照片,并对其进行统一尺寸裁剪

news/2024/10/17 4:51:34/

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();//重点,必须加上,否则父子窗体都假死}
}

}


http://www.ppmy.cn/news/51213.html

相关文章

局域网 - 高速以太网(百兆、千兆、万兆)

文章目录 1 概述1.1 802.3 物理层规范1.2 以太网标准中后缀 -T、-F、-X 含义 2 分类2.1 快速以太网&#xff08;802.3μ、百兆&#xff09;2.2 千兆以太网&#xff08;802.3z、802.3ab&#xff09;2.3 万兆以太网&#xff08;802.3ae&#xff09; 3 扩展3.1 网工软考真题 1 概述…

Java --- springboot2自动配置原理

目录 一、自动配置原理 1.1、引导加载类配置 1.2、按需开启自动配置项 1.3、修改默认配置 一、自动配置原理 1.1、引导加载类配置 SpringBootApplication注解其实就是以下三个核心注解合并而成 SpringBootConfiguration EnableAutoConfiguration ComponentScan(excludeFil…

sqlacodegen生成SQLAlchemy模型

SQLAlchemy是一个Python SQL工具包和ORM框架&#xff0c;它提供了一种方便的方式来与关系型数据库进行交互。sqlacodegen是一个用于生成SQLAlchemy模型代码的命令行工具&#xff0c;它可以根据现有的数据库表结构自动生成Python代码。 生成MySQL模型代 可以通过以下命令生成M…

【MySQL高级】——目录结构数据库和文件系统的关系

一、目录结构 <1> 主要目录结构 find / -name mysql<2> 数据库文件目录 目录&#xff1a;/var/lib/mysql/ 配置方式&#xff1a;show variables like ‘datadir’; <3> 相关命令目录 目录&#xff1a;/usr/bin&#xff08;mysqladmin、mysqlbinlog、my…

【Vue 移动端开发】适配百分之99的屏幕方案

一、引入 之前提起移动端适配&#xff0c;都是一些视口的概念&#xff0c;包括物理像素和逻辑像素&#xff0c;理想视口&#xff0c;dpr等等等。利用 media query 和 rem 是最常见的移动端适配方案。如下代码&#xff1a; const deviceWidth document.documentElement.clien…

AcWing第 100 场周赛

4953. 比赛 n 个人参加乒乓球比赛。 比赛一共进行了 n−1 场。 每场比赛举办方都需要准备 2b1 瓶水&#xff0c;其中 2b 瓶水给选手&#xff08;每场比赛 2 人参加&#xff0c;每人 b 瓶&#xff09;&#xff0c;1 瓶水给裁判。 此外&#xff0c;所有参加比赛的 n 名选手&a…

代码不朽的读书笔记——软件的可维护性

软件的可维护性 软件维护的四种类型 纠正性维护 : 这是改BUG 适应性维护 : 这是更换操作系统与数据库等外部环境时的修改 完善性维护 : 这是有新需求的修改 预防性维护 : 确定可以改进质量或者是预防未来出现的BUG的修改 1. 坚持简单的原则最有助于提高可维护性. 2. 可维护…

centos7操作yum命令失败

前言设置网卡开机自动启动设置国内dns服务器系统修改CentOS-Base.repo中的地址 前言 刚安装完的CentOS7的系统&#xff0c;发现无法使用yum命令进行更新&#xff0c;在更新的时候会出现下面这种内容&#xff0c;为此问题有以下这些解决方案可以尝试。 One of the configured r…