C# 实现的 批量彩色图片转黑白图片的小工具 (附源码)

本文分享了一段C#代码,用于将大量彩色图片批量转换为黑白图片。该程序支持选择源路径和目标路径,并提供了进度条显示处理进度。代码简单易懂,适合初学者学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为工作需要将大量的彩色图片转换成黑白图片,所以索性自己花了点时间,自己写了一个,为了图方便,就用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.youkuaiyun.com/detail/toss156/4300035 压缩的有问题,重新上传了一个可以正常解压的。。

http://download.youkuaiyun.com/detail/toss156/4303127

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值