c# 通過現(xiàn)在文件夾,獲取下面所有的照片,并對(duì)其進(jìn)行統(tǒng)一尺寸裁剪
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();
}文章來源:http://www.zghlxwxcb.cn/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);
//創(chuàng)建矩形對(duì)象表示原圖上裁剪的矩形區(qū)域,這里相當(dāng)于劃定原圖上坐標(biāo)為(10, 10)處,50x50大小的矩形區(qū)域?yàn)椴眉魠^(qū)域
Rectangle cropRegion = new Rectangle(0, 0, 900, 768);
//創(chuàng)建空白畫布,大小為裁剪區(qū)域大小
Bitmap result = new Bitmap(cropRegion.Width, cropRegion.Height);
//創(chuàng)建Graphics對(duì)象,并指定要在result(目標(biāo)圖片畫布)上繪制圖像
Graphics graphics = Graphics.FromImage(result);
//使用Graphics對(duì)象把原圖指定區(qū)域圖像裁剪下來并填充進(jìn)剛剛創(chuàng)建的空白畫布
graphics.DrawImage(originImage, new Rectangle(0, 0, cropRegion.Width, cropRegion.Height), cropRegion, GraphicsUnit.Pixel);
//這個(gè)時(shí)候裁剪區(qū)域圖片就被填充進(jìn)result對(duì)象中去了,可以對(duì)其進(jìn)行保存
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//對(duì)于子目錄,進(jìn)行遞歸調(diào)用
selecte_jpg(files[i]);
}
return listPics;
}
private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "請(qǐng)選擇文件路徑";
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) //設(shè)置進(jìn)度條當(dāng)前進(jìn)度值
{
if (value < progressBar1.Maximum + 1) //如果值有效
{
progressBar1.Value = value; //設(shè)置進(jìn)度值
}
Application.DoEvents();//重點(diǎn),必須加上,否則父子窗體都假死
}
}
}文章來源地址http://www.zghlxwxcb.cn/news/detail-425908.html
到了這里,關(guān)于c# 通過現(xiàn)在文件夾,獲取下面所有的照片,并對(duì)其進(jìn)行統(tǒng)一尺寸裁剪的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!