实例介绍
【实例简介】图片批量缩放程序
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Windows.Forms; using System.Threading; namespace ThumbnailImages { public delegate void DelegateStart(); public delegate void DelegateDoException(Exception ex); public delegate void DelegateThreadFinished(string message); public delegate void DelegateProgressBarPerformStep(int fileIndex, string filename); public partial class frmMain : Form { int width = 1024, height = 768; int percent = -1; int totalFileCount = 0; private Thread timedProgress; string sourcePath, targetPath; string[] files; public DelegateStart DelegateStart; public DelegateDoException DelegateDoException; public DelegateThreadFinished DelegateThreadFinished; public DelegateProgressBarPerformStep DelegateProgressBarPerformStep; public frmMain() { InitializeComponent(); this.cbDefined.SelectedIndex = 2; DelegateStart = new DelegateStart(this.Start); DelegateThreadFinished = new DelegateThreadFinished(this.ThreadFinished); DelegateDoException = new DelegateDoException(this.DoException); DelegateProgressBarPerformStep = new DelegateProgressBarPerformStep(this.ProgressBarPerformStep); } private void btnStart_Click(object sender, EventArgs e) { string sourcePath = txtSource.Text; string targetPath = txtTarget.Text; if (sourcePath.Length == 0 || !Directory.Exists(sourcePath)) { txtSource.Focus(); MessageBox.Show("请重新选择图片文件夹!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (targetPath.Length == 0) { txtTarget.Focus(); MessageBox.Show("请重新选择缩略图保存路径!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (!Directory.Exists(targetPath)) { try { Directory.CreateDirectory(targetPath); } catch { txtTarget.Focus(); MessageBox.Show("请重新选择缩略图保存路径!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } GetImageSizeSetting(); //TimedProgressProc(); timedProgress = new Thread(new ThreadStart(TimedProgressProc)); timedProgress.IsBackground = true; timedProgress.Start(); } public void Start() { sourcePath = txtSource.Text; targetPath = txtTarget.Text; files = Directory.GetFiles(sourcePath); totalFileCount = files.Length; this.lblInfo.Text = string.Format("0/{0}", totalFileCount); this.progressBar.Maximum = totalFileCount; this.btnStart.Enabled = false; this.btnStop.Enabled = true; } public void ThreadFinished(string message) { if (MessageBox.Show(message, "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { System.Diagnostics.Process.Start(targetPath); } this.btnStart.Enabled = true; this.btnStop.Enabled = false; this.progressBar.Value = 0; this.lblInfo.Text = "全部完成!"; } public void DoException(Exception ex) { MessageBox.Show(ex.ToString(), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); this.btnStart.Enabled = true; this.btnStop.Enabled = false; this.progressBar.Value = 0; } public void ProgressBarPerformStep(int fileIndex, string filename) { progressBar.PerformStep(); filename = Path.GetFileName(filename); this.lblInfo.Text = string.Format("{0}/{1}, 当前文件: {2}", fileIndex, totalFileCount, filename); } private void TimedProgressProc() { try { ImageCodecInfo icf = ImageHelper.GetImageCodecInfo(ImageFormat.Jpeg); Image image, thumbnailImage; string thumbnailImageFilename; this.Invoke(this.DelegateStart); int i = 0; foreach (string filename in files) { if (timedProgress == null) return; //lblInfo.Text = string.Format("正在处理: {0} ", filename); ; progressBar.Invoke(this.DelegateProgressBarPerformStep, new object[] { i , filename }); string extension = Path.GetExtension(filename).ToLower(); if (extension != ".jpg" && extension != ".jpeg" && extension != ".gif" && extension != ".bmp" && extension != ".png" ) continue; try { using (image = Image.FromFile(filename)) { Size imageSize = GetImageSize(image); using (thumbnailImage = ImageHelper.GetThumbnailImage(image, imageSize.Width, imageSize.Height)) { thumbnailImageFilename = Path.GetFileNameWithoutExtension(filename) "_" thumbnailImage.Width "x" thumbnailImage.Height extension; ImageHelper.SaveImage(thumbnailImage, Path.Combine(targetPath, thumbnailImageFilename), icf); } } } catch(Exception ex) { string em = ex.Message; //lblInfo.Text = string.Format("{0} 可能不是合法的图片文件!", filename); } } this.Invoke(this.DelegateThreadFinished, new object[] { "已经全部完成!\n是否打开生成的缩略图所在文件夹?" }); } catch (ThreadInterruptedException e) { return; } catch (Exception ex) { this.Invoke(this.DelegateDoException, new Object[] { ex }); } } private Size GetImageSize(Image picture) { Size imageSize; if (percent > 0) { imageSize = new Size(picture.Width * percent / 100, picture.Height * percent / 100); } else imageSize = new Size(width, height); double heightRatio = (double)picture.Height / picture.Width; double widthRatio = (double)picture.Width / picture.Height; int desiredHeight = imageSize.Height; int desiredWidth = imageSize.Width; imageSize.Height = desiredHeight; if (widthRatio > 0) imageSize.Width = Convert.ToInt32(imageSize.Height * widthRatio); if (imageSize.Width > desiredWidth) { imageSize.Width = desiredWidth; imageSize.Height = Convert.ToInt32(imageSize.Width * heightRatio); } return imageSize; } private void GetImageSizeSetting() { if (rbDefined.Checked) { if (cbDefined.SelectedIndex == 0) { width = 1024; height = 768; } else if (cbDefined.SelectedIndex == 1) { width = 800; height = 600; } else if (cbDefined.SelectedIndex == 2) { width = 640; height = 480; } else if (cbDefined.SelectedIndex == 3) { width = 448; height = 336; } else if (cbDefined.SelectedIndex == 4) { width = 314; height = 235; } else if (cbDefined.SelectedIndex == 5) { width = 160; height = 160; } } else if (rbCustom.Checked) { width = (int)numWidth.Value; height = (int)numHeight.Value; } else if (rbDefined.Checked) { percent = (int)numPercent.Value; } } private void cbDefined_SelectedIndexChanged(object sender, EventArgs e) { if (!rbDefined.Checked) rbDefined.Checked = true; } private void btnBrowserSource_Click(object sender, EventArgs e) { if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { txtSource.Text = folderBrowserDialog.SelectedPath; if (txtTarget.Text.Length == 0) txtTarget.Text = Path.Combine(folderBrowserDialog.SelectedPath, "ThumbnailImages"); } } private void btnBrowserTarget_Click(object sender, EventArgs e) { if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { txtTarget.Text = folderBrowserDialog.SelectedPath; } } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { // Change the color of the link text by setting LinkVisited // to true. linkLabel1.LinkVisited = true; //Call the Process.Start method to open the default browser //with a URL: System.Diagnostics.Process.Start("http://www.openlab.net.cn"); } private void btnStop_Click(object sender, EventArgs e) { if (timedProgress != null) { timedProgress.Interrupt(); timedProgress = null; ThreadFinished("已经取消!\n是否打开生成的缩略图所在文件夹?"); this.lblInfo.Text = "已取消!"; } } } }
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论