C# 实现的 批量彩色图片转黑白图片的小工具 (附源码)
2012-05-14 10:13
501 查看
因为工作需要将大量的彩色图片转换成黑白图片,所以索性自己花了点时间,自己写了一个,为了图方便,就用C#开发,代码很简单,实现了选择源路径,目标路径,覆盖源路径,有进度条显示。没有专门学过C#,所以代码写的不怎么样。。希望大家谅解---
(注:因为是用VS2010开发的,所以需要安装.Net Framework4.0 才能运行。。)
工具下载地址:
http://download.csdn.net/detail/toss156/4300035 压缩的有问题,重新上传了一个可以正常解压的。。
http://download.csdn.net/detail/toss156/4303127
(注:因为是用VS2010开发的,所以需要安装.Net Framework4.0 才能运行。。)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { static IList<string> path = new List<string>(); //保存你图片名称 Bitmap curBitmap = null; Bitmap resBitmap = null; int count = 0; public Form1() { InitializeComponent(); } private void sourcebtn_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择文件路径"; if (dialog.ShowDialog() == DialogResult.OK) { sourceStr.Text = dialog.SelectedPath; } } private void resultbtn_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择文件路径"; if (dialog.ShowDialog() == DialogResult.OK) { resultStr.Text = dialog.SelectedPath; } } private void startbtn_Click(object sender, EventArgs e) { DirectoryInfo di = new DirectoryInfo(sourceStr.Text); //获取该路径下的所有文件的列表 FileInfo[] fileInfo = di.GetFiles(); path.Clear(); //开始得到图片名称 foreach (FileInfo subinfo in fileInfo) { //判断扩展名是否相同 if (subinfo.Extension == ".jpg" || subinfo.Extension == ".JPG" || subinfo.Extension == ".jpeg" || subinfo.Extension == ".JPEG") { string strname = subinfo.Name; //获取文件名称 path.Add(strname); //把文件名称保存在泛型集合中 info.Text = "找到" + path.Count + "张图片"; } } if (path.Count>0) { progressBar1.Value = 0; progressBar1.Step = (int)100 / path.Count; new System.Threading.Thread(new System.Threading.ThreadStart(StartTransform)).Start(); } else { MessageBox.Show("没有找到JPG图片文件"); } } delegate void MyDelegate(int param); void DelegateMethod(int param) { total.Text = "正在处理第" + (count + 1) + "/" + path.Count + "张图片"; if ((count+1) == path.Count) { progressBar1.Value = 100; total.Text = path.Count+"张图片处理完成"; }else { progressBar1.PerformStep(); } } public void StartTransform() { int ih ,iw; string filename; for (count = 0; count < path.Count; count++ ) { try { filename = path[count]; pictureBox1.Refresh(); Image image =Image.FromFile(sourceStr.Text + "\\" + filename); curBitmap = new Bitmap(image); image.Dispose(); pictureBox1.Image = curBitmap; iw = pictureBox1.Image.Width; ih = pictureBox1.Image.Height; resBitmap = toGray(curBitmap, iw, ih); pictureBox2.Refresh(); pictureBox2.Image = resBitmap; if (isCover.Checked) { pictureBox2.Image.Save(sourceStr.Text + "\\" + filename); //覆盖源文件 } else { pictureBox2.Image.Save(resultStr.Text + "\\" + filename); //设置保存路径 } //更新UI界面 this.Invoke(new MyDelegate(DelegateMethod), count); } catch (System.Exception ex) { MessageBox.Show(ex.ToString()); } } } public Bitmap toGray(Bitmap bm, int iw, int ih) { Color c = new Color(); int t; //转变为灰度图像矩阵 for (int j = 0; j < ih; j++) { for (int i = 0; i < iw; i++) { c = bm.GetPixel(i, j); t = (int)((c.R + c.G + c.B) / 3.0); bm.SetPixel(i, j, Color.FromArgb(t, t, t)); } } return bm; } private void isCover_CheckedChanged(object sender, EventArgs e) { if(isCover.Checked) { resultStr.Enabled = false; resultbtn.Enabled = false; }else { resultStr.Enabled = true; resultbtn.Enabled = true; } } } }
工具下载地址:
http://download.csdn.net/detail/toss156/4300035 压缩的有问题,重新上传了一个可以正常解压的。。
http://download.csdn.net/detail/toss156/4303127
相关文章推荐
- 【小工具】将压缩包隐藏进图片中(C#源码,拖拽实现)
- .net+ajax+jquery.form实现简单的图片批量上传 含Demo源码下载
- c#实现图片二值化例子(黑白效果)
- JavaScript+Canvas实现彩色图片转换成黑白图片的方法分析
- C# 批量图片合并工具(附源代码)
- 用C#开发一个WinForm版的批量图片压缩工具
- C# sql 实现图片批量储存读取
- asp.net(c#)编程实现将彩色图片变灰阶图片的方法示例
- C# 图片处理之:彩色图片转为黑白图
- c#实现一个超实用的证件照换底色小工具(附源码)
- C# 图片处理之:彩色图片转为黑白图 .
- 用C#开发一个WinForm版的批量图片压缩工具
- C#实现外部图片的拖拽到应用程序的简单功能,附全部源码,供有需要的参考
- 浅谈JavaScript和Canvas实现彩色图片转换成黑白图片
- C# 图片处理之:彩色图片转为黑白图
- 发布C#版本 图片批量追加水印工具
- c#实现图片二值化例子(黑白效果)
- c#实现网页图片提取工具代码分享
- 分享PHP源码批量抓取远程网页图片并保存到本地的实现方法
- 用C#开发一个WinForm版的批量图片压缩工具