C# 实现的 批量彩色图片转黑白图片的小工具 (附源码)
来源:互联网 发布:visio模板 软件架构 编辑:程序博客网 时间:2024/04/30 09:56
因为工作需要将大量的彩色图片转换成黑白图片,所以索性自己花了点时间,自己写了一个,为了图方便,就用C#开发,代码很简单,实现了选择源路径,目标路径,覆盖源路径,有进度条显示。没有专门学过C#,所以代码写的不怎么样。。希望大家谅解---
(注:因为是用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# 实现的 批量彩色图片转黑白图片的小工具 (附源码)
- c# 彩色图片变为黑白图片
- 把彩色图片变成适合素描的黑白图片
- 业余写的一个小工具_XPathTool(C#源码)
- Windows虚拟地址转物理地址(原理+源码实现,附简单小工具)
- 自制的批量文件复制小工具
- 批量转文本编码的小工具
- 一键拷贝命令小工具(附源码)
- sql自动批量产生数据小工具的主页面源码
- 手把手VC截图小工具,附源码
- DELPHI写的截图小工具源码(部分)
- C#写的浮点或汉字转十六进制小工具
- 【小工具】将压缩包隐藏进图片中(C#源码,拖拽实现)
- 实现统计java程序代码的小工具
- VBS实现的日程提醒小工具
- Python 实现的关键词查找小工具
- 找到一款批量清除Squid缓存的小工具
- J2SE批量生成J2ME的Jad小工具
- Hook IE浏览器alert弹窗
- AndroidManifest.xml文件详解(intent-filter)
- Oracle存储过程调用带参的存储过程
- “活不下去”的中产阶级
- 什么时候用trycatch
- C# 实现的 批量彩色图片转黑白图片的小工具 (附源码)
- SQL SERVER 2005 EXPRESS 配置静态端口访问
- Flash移动开发高级教程——创建Anroid & iPhone应用
- Linux udev-用户空间设备管理器
- ios5 代码获取storyboard文件的对象
- 如何成为一名黑客 from Eric Raymond
- 八款开源 Android 游戏引擎 (巨好的资源)
- 将Tomcat加入服务,实现开机自动运行
- Oracle的多个数据库的联合查询(包含同义词)