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

原创粉丝点击