利用C#实现批量图片格式转换功能

.NET
324
0
0
2023-07-01
标签   C#
目录
  • 实践过程
  • 效果
  • 代码

实践过程

效果

代码

public partial class Form : Form
{
    public Form()
    {
        InitializeComponent();
    }

    string[] path = null; //用于存储选择的文件列表
    string path = ""; //用于存储保存的路径
    Bitmap bt; //声明一个转换图片格式的Bitmap对象
    Thread td; //声明一个线程
    int Imgtype; //声明一个变量用于标记ConvertImage方法中转换的类型
    string OlePath; //声明一个变量用于存储ConvertImage方法中原始图片的路径
    string path; //声明一个变量用于存储ConvertImage方法中转换后图片的保存路径
    int flags; //用于标记已转换图片的数量,用于计算转换进度

    private void Form_Load(object sender, EventArgs e)
    {
        tscbType.SelectedIndex =; //设置第一个转换类型被选中
        CheckForIllegalCrossThreadCalls = false; //屏蔽线程弹出的错误提示
    }

    private void toolStripButton_Click(object sender, EventArgs e) //选择转换文件的按钮
    {
        if (openFileDialog.ShowDialog() == DialogResult.OK) //判断是否选择文件
        {
            listView.Items.Clear(); //清空listView1
            string[] info = new string[]; //存储每一行数据
            FileInfo fi; //创建一个FileInfo对象,用于获取图片信息
            path = openFileDialog1.FileNames; //获取选择的图片集合
            for (int i =; i < path1.Length; i++) //读取集合中的内容
            {
                //获取图片名称
                string ImgName = path[i].Substring(path1[i].LastIndexOf("\\") + 1,
                    path[i].Length - path1[i].LastIndexOf("\\") - 1);
                //获取图片类型
                string ImgType = ImgName.Substring(ImgName.LastIndexOf(".") +,
                    ImgName.Length - ImgName.LastIndexOf(".") -);
                fi = new FileInfo(path[i].ToString()); //实例化FileInfo对象
                //将每一行数据第一个位置的图标添加到imageList中
                imageList.Images.Add(ImgName, Properties.Resources.图标__23_);
                info[] = ImgName; //图片名称
                info[] = ImgType; //图片类型
                info[] = fi.LastWriteTime.ToShortDateString(); //图片最后修改日期
                info[] = path1[i].ToString(); //图片位置
                info[] = (fi.Length / 1024) + "KB"; //图片大小
                info[] = "未转换"; //图片状态
                ListViewItem lvi = new ListViewItem(info, ImgName); //实例化ListViewItem对象
                listView.Items.Add(lvi); //将信息添加到listView1控件中
            }

            tsslFileNum.Text = "当前共有" + path.Length.ToString() + "个文件"; //状态栏中显示图片数量
        }
    }

    private void toolStripButton_Click(object sender, EventArgs e) //关闭按钮
    {
        Application.Exit(); //退出系统
    }

    private void toolStripButton_Click(object sender, EventArgs e) //清空列表的按钮
    {
        listView.Items.Clear(); //清空列表
        path = null; //清空图片的集合
        tsslFileNum.Text = "当前没有文件"; //状态栏中提示
        tsslPlan.Text = ""; //清空进度数字
    }

    private void toolStripButton_Click(object sender, EventArgs e)
    {
        if (path == null) //判断是否选择图片
        {
            MessageBox.Show("请选择图片!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else
        {
            if (path.Length == 0) //判断是否选择保存位置
            {
                MessageBox.Show("请选择保存位置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                flags =; //初始化flags变量为1,用于计算进度
                toolStrip.Enabled = false; //当转换开始时,禁用工具栏
                int flag = tscbType.SelectedIndex; //判断将图片转换为何种格式
                switch (flag) //根据不同的格式进行转换
                {
                    case:
                        Imgtype = 0; //如果选择第一项则转换为BMP格式
                        td = new Thread(new ThreadStart(ConvertImage)); //通过线程调用ConvertImage方法进行转换
                        td.Start();
                        break;
                    case: //如果选择第二项则转换为JPG格式
                        Imgtype = 1;
                        td = new Thread(new ThreadStart(ConvertImage)); //通过线程调用ConvertImage方法进行转换
                        td.Start();
                        break;
                    case: //如果选择第三项则转换为PNG格式
                        Imgtype = 2;
                        td = new Thread(new ThreadStart(ConvertImage)); //通过线程调用ConvertImage方法进行转换
                        td.Start();
                        break;
                    case: //如果选择第四项则转换为GIF格式
                        Imgtype = 3;
                        td = new Thread(new ThreadStart(ConvertImage)); //通过线程调用ConvertImage方法进行转换
                        td.Start();
                        break;
                    default:
                        td.Abort();
                        break;
                }
            }
        }
    }

    private void toolStripButton_Click(object sender, EventArgs e) //选择保存路径按钮
    {
        if (folderBrowserDialog.ShowDialog() == DialogResult.OK) //判断是否选择保存路径
        {
            path = folderBrowserDialog1.SelectedPath; //获取保存路径
        }
    }

    private void ConvertImage()
    {
        flags =;
        switch (Imgtype)
        {
            case:
                for (int i =; i < path1.Length; i++)
                {
                    string ImgName = path[i].Substring(path1[i].LastIndexOf("\\") + 1,
                        path[i].Length - path1[i].LastIndexOf("\\") - 1);
                    ImgName = ImgName.Remove(ImgName.LastIndexOf("."));
                    OlePath = path[i].ToString();
                    bt = new Bitmap(OlePath);
                    path = path + "\\" + ImgName + ".bmp";
                    bt.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
                    listView.Items[flags - 1].SubItems[6].Text = "已转换";
                    tsslPlan.Text = "正在转换" + flags * / path1.Length + "%";
                    if (flags == path.Length)
                    {
                        toolStrip.Enabled = true;
                        tsslPlan.Text = "图片转换全部完成";
                    }

                    flags++;
                }

                break;
            case:
                for (int i =; i < path1.Length; i++)
                {
                    string ImgName = path[i].Substring(path1[i].LastIndexOf("\\") + 1,
                        path[i].Length - path1[i].LastIndexOf("\\") - 1);
                    ImgName = ImgName.Remove(ImgName.LastIndexOf("."));
                    OlePath = path[i].ToString();
                    bt = new Bitmap(OlePath);
                    path = path + "\\" + ImgName + ".jpeg";
                    bt.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
                    listView.Items[flags - 1].SubItems[6].Text = "已转换";
                    tsslPlan.Text = "正在转换" + flags * / path1.Length + "%";
                    if (flags == path.Length)
                    {
                        toolStrip.Enabled = true;
                        tsslPlan.Text = "图片转换全部完成";
                    }

                    flags++;
                }

                break;
            case:
                for (int i =; i < path1.Length; i++)
                {
                    string ImgName = path[i].Substring(path1[i].LastIndexOf("\\") + 1,
                        path[i].Length - path1[i].LastIndexOf("\\") - 1);
                    ImgName = ImgName.Remove(ImgName.LastIndexOf("."));
                    OlePath = path[i].ToString();
                    bt = new Bitmap(OlePath);
                    path = path + "\\" + ImgName + ".png";
                    bt.Save(path, System.Drawing.Imaging.ImageFormat.Png);
                    listView.Items[flags - 1].SubItems[6].Text = "已转换";
                    tsslPlan.Text = "正在转换" + flags * / path1.Length + "%";
                    if (flags == path.Length)
                    {
                        toolStrip.Enabled = true;
                        tsslPlan.Text = "图片转换全部完成";
                    }

                    flags++;
                }

                break;
            case:
                for (int i =; i < path1.Length; i++)
                {
                    string ImgName = path[i].Substring(path1[i].LastIndexOf("\\") + 1,
                        path[i].Length - path1[i].LastIndexOf("\\") - 1);
                    ImgName = ImgName.Remove(ImgName.LastIndexOf("."));
                    OlePath = path[i].ToString();
                    bt = new Bitmap(OlePath);
                    path = path + "\\" + ImgName + ".gif";
                    bt.Save(path, System.Drawing.Imaging.ImageFormat.Gif);
                    listView.Items[flags - 1].SubItems[6].Text = "已转换";
                    tsslPlan.Text = "正在转换" + flags * / path1.Length + "%";
                    if (flags == path.Length)
                    {
                        toolStrip.Enabled = true;
                        tsslPlan.Text = "图片转换全部完成";
                    }

                    flags++;
                }

                break;
            default:
                bt.Dispose();
                break;
        }
    }

    private void Form_FormClosed(object sender, FormClosedEventArgs e) //关闭窗口时要关闭线程
    {
        if (td != null) //判断是否存在线程
        {
            if (td.ThreadState == ThreadState.Running) //然后判断线程是否正在运行
            {
                td.Abort(); //如果运行则关闭线程
            }
        }
    }
}
partial class Form
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form));
        this.toolStrip = new System.Windows.Forms.ToolStrip();
        this.toolStripLabel = new System.Windows.Forms.ToolStripLabel();
        this.toolStripButton = new System.Windows.Forms.ToolStripButton();
        this.toolStripSeparator = new System.Windows.Forms.ToolStripSeparator();
        this.toolStripButton = new System.Windows.Forms.ToolStripButton();
        this.toolStripSeparator = new System.Windows.Forms.ToolStripSeparator();
        this.toolStripLabel = new System.Windows.Forms.ToolStripLabel();
        this.tscbType = new System.Windows.Forms.ToolStripComboBox();
        this.toolStripSeparator = new System.Windows.Forms.ToolStripSeparator();
        this.toolStripButton = new System.Windows.Forms.ToolStripButton();
        this.toolStripSeparator = new System.Windows.Forms.ToolStripSeparator();
        this.toolStripButton = new System.Windows.Forms.ToolStripButton();
        this.toolStripSeparator = new System.Windows.Forms.ToolStripSeparator();
        this.toolStripButton = new System.Windows.Forms.ToolStripButton();
        this.statusStrip = new System.Windows.Forms.StatusStrip();
        this.tsslFileNum = new System.Windows.Forms.ToolStripStatusLabel();
        this.toolStripStatusLabel = new System.Windows.Forms.ToolStripStatusLabel();
        this.tsslPlan = new System.Windows.Forms.ToolStripStatusLabel();
        this.listView = new System.Windows.Forms.ListView();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.imageList = new System.Windows.Forms.ImageList(this.components);
        this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
        this.folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
        this.toolStrip.SuspendLayout();
        this.statusStrip.SuspendLayout();
        this.SuspendLayout();
        // 
        // toolStrip
        // 
        this.toolStrip.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
        this.toolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.toolStripLabel,
        this.toolStripButton,
        this.toolStripSeparator,
        this.toolStripButton,
        this.toolStripSeparator,
        this.toolStripLabel,
        this.tscbType,
        this.toolStripSeparator,
        this.toolStripButton,
        this.toolStripSeparator,
        this.toolStripButton,
        this.toolStripSeparator,
        this.toolStripButton});
        this.toolStrip.Location = new System.Drawing.Point(0, 0);
        this.toolStrip.Name = "toolStrip1";
        this.toolStrip.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
        this.toolStrip.Size = new System.Drawing.Size(536, 25);
        this.toolStrip.TabIndex = 0;
        this.toolStrip.Text = "toolStrip1";
        // 
        // toolStripLabel
        // 
        this.toolStripLabel.Name = "toolStripLabel1";
        this.toolStripLabel.Size = new System.Drawing.Size(17, 22);
        this.toolStripLabel.Text = "  ";
        // 
        // toolStripButton
        // 
        this.toolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
        this.toolStripButton.Image = global::PictureBatchConversion.Properties.Resources.打开;
        this.toolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.toolStripButton.Name = "toolStripButton3";
        this.toolStripButton.Size = new System.Drawing.Size(23, 22);
        this.toolStripButton.Text = "toolStripButton3";
        this.toolStripButton.ToolTipText = "选择需要转换的图片";
        this.toolStripButton.Click += new System.EventHandler(this.toolStripButton3_Click);
        // 
        // toolStripSeparator
        // 
        this.toolStripSeparator.Name = "toolStripSeparator1";
        this.toolStripSeparator.Size = new System.Drawing.Size(6, 25);
        // 
        // toolStripButton
        // 
        this.toolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
        this.toolStripButton.Image = global::PictureBatchConversion.Properties.Resources.图标__29_;
        this.toolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.toolStripButton.Name = "toolStripButton4";
        this.toolStripButton.Size = new System.Drawing.Size(23, 22);
        this.toolStripButton.Text = "toolStripButton4";
        this.toolStripButton.TextImageRelation = System.Windows.Forms.TextImageRelation.Overlay;
        this.toolStripButton.ToolTipText = "选择保存位置";
        this.toolStripButton.Click += new System.EventHandler(this.toolStripButton4_Click);
        // 
        // toolStripSeparator
        // 
        this.toolStripSeparator.Name = "toolStripSeparator2";
        this.toolStripSeparator.Size = new System.Drawing.Size(6, 25);
        // 
        // toolStripLabel
        // 
        this.toolStripLabel.Name = "toolStripLabel3";
        this.toolStripLabel.Size = new System.Drawing.Size(65, 22);
        this.toolStripLabel.Text = "转换格式:";
        // 
        // tscbType
        // 
        this.tscbType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
        this.tscbType.FlatStyle = System.Windows.Forms.FlatStyle.System;
        this.tscbType.Items.AddRange(new object[] {
        "转换为BMP格式",
        "转换为JPG格式",
        "转换为PNG格式",
        "转换为GIF格式"});
        this.tscbType.Name = "tscbType";
        this.tscbType.Size = new System.Drawing.Size(, 25);
        // 
        // toolStripSeparator
        // 
        this.toolStripSeparator.Name = "toolStripSeparator3";
        this.toolStripSeparator.Size = new System.Drawing.Size(6, 25);
        // 
        // toolStripButton
        // 
        this.toolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.toolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton1.Image")));
        this.toolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.toolStripButton.Name = "toolStripButton1";
        this.toolStripButton.Size = new System.Drawing.Size(57, 22);
        this.toolStripButton.Text = "开始转换";
        this.toolStripButton.Click += new System.EventHandler(this.toolStripButton1_Click);
        // 
        // toolStripSeparator
        // 
        this.toolStripSeparator.Name = "toolStripSeparator4";
        this.toolStripSeparator.Size = new System.Drawing.Size(6, 25);
        // 
        // toolStripButton
        // 
        this.toolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.toolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton5.Image")));
        this.toolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.toolStripButton.Name = "toolStripButton5";
        this.toolStripButton.Size = new System.Drawing.Size(57, 22);
        this.toolStripButton.Text = "清空列表";
        this.toolStripButton.Click += new System.EventHandler(this.toolStripButton5_Click);
        // 
        // toolStripSeparator
        // 
        this.toolStripSeparator.Name = "toolStripSeparator5";
        this.toolStripSeparator.Size = new System.Drawing.Size(6, 25);
        // 
        // toolStripButton
        // 
        this.toolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.toolStripButton.Font = new System.Drawing.Font("宋体", 9F);
        this.toolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton2.Image")));
        this.toolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.toolStripButton.Name = "toolStripButton2";
        this.toolStripButton.Size = new System.Drawing.Size(33, 22);
        this.toolStripButton.Text = "关闭";
        this.toolStripButton.Click += new System.EventHandler(this.toolStripButton2_Click);
        // 
        // statusStrip
        // 
        this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.tsslFileNum,
        this.toolStripStatusLabel,
        this.tsslPlan});
        this.statusStrip.Location = new System.Drawing.Point(0, 321);
        this.statusStrip.Name = "statusStrip1";
        this.statusStrip.Size = new System.Drawing.Size(536, 22);
        this.statusStrip.TabIndex = 1;
        this.statusStrip.Text = "statusStrip1";
        // 
        // tsslFileNum
        // 
        this.tsslFileNum.Name = "tsslFileNum";
        this.tsslFileNum.Size = new System.Drawing.Size(, 17);
        // 
        // toolStripStatusLabel
        // 
        this.toolStripStatusLabel.Name = "toolStripStatusLabel1";
        this.toolStripStatusLabel.Size = new System.Drawing.Size(23, 17);
        this.toolStripStatusLabel.Text = "   ";
        // 
        // tsslPlan
        // 
        this.tsslPlan.Name = "tsslPlan";
        this.tsslPlan.Size = new System.Drawing.Size(, 17);
        // 
        // listView
        // 
        this.listView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
        this.columnHeader,
        this.columnHeader,
        this.columnHeader,
        this.columnHeader,
        this.columnHeader,
        this.columnHeader,
        this.columnHeader});
        this.listView.Dock = System.Windows.Forms.DockStyle.Fill;
        this.listView.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        this.listView.FullRowSelect = true;
        this.listView.GridLines = true;
        this.listView.Location = new System.Drawing.Point(0, 25);
        this.listView.Name = "listView1";
        this.listView.Size = new System.Drawing.Size(536, 296);
        this.listView.SmallImageList = this.imageList1;
        this.listView.TabIndex = 2;
        this.listView.UseCompatibleStateImageBehavior = false;
        this.listView.View = System.Windows.Forms.View.Details;
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "";
        this.columnHeader.Width = 20;
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "文件名";
        this.columnHeader.Width = 120;
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "扩展名";
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "日期";
        this.columnHeader.Width = 80;
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "路径";
        this.columnHeader.Width = 120;
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "大小";
        this.columnHeader.Width = 70;
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "状态";
        // 
        // imageList
        // 
        this.imageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
        this.imageList.ImageSize = new System.Drawing.Size(16, 16);
        this.imageList.TransparentColor = System.Drawing.Color.Transparent;
        // 
        // openFileDialog
        // 
        this.openFileDialog.Filter = "所有图片|*.jpg;*.jpeg;*.gif;*.bmp;*.png";
        this.openFileDialog.Multiselect = true;
        // 
        // Form
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(, 343);
        this.Controls.Add(this.listView);
        this.Controls.Add(this.statusStrip);
        this.Controls.Add(this.toolStrip);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
        this.MaximizeBox = false;
        this.Name = "Form";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "批量图片格式转换";
        this.Load += new System.EventHandler(this.Form_Load);
        this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form_FormClosed);
        this.toolStrip.ResumeLayout(false);
        this.toolStrip.PerformLayout();
        this.statusStrip.ResumeLayout(false);
        this.statusStrip.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.ToolStrip toolStrip;
    private System.Windows.Forms.StatusStrip statusStrip;
    private System.Windows.Forms.ListView listView;
    private System.Windows.Forms.ToolStripLabel toolStripLabel;
    private System.Windows.Forms.ToolStripSeparator toolStripSeparator;
    private System.Windows.Forms.ToolStripLabel toolStripLabel;
    private System.Windows.Forms.ToolStripComboBox tscbType;
    private System.Windows.Forms.ToolStripSeparator toolStripSeparator;
    private System.Windows.Forms.ToolStripSeparator toolStripSeparator;
    private System.Windows.Forms.ToolStripButton toolStripButton;
    private System.Windows.Forms.ToolStripButton toolStripButton;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.OpenFileDialog openFileDialog;
    private System.Windows.Forms.ToolStripButton toolStripButton;
    private System.Windows.Forms.ToolStripButton toolStripButton;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.ImageList imageList;
    private System.Windows.Forms.ToolStripStatusLabel tsslFileNum;
    private System.Windows.Forms.ToolStripButton toolStripButton;
    private System.Windows.Forms.ToolStripSeparator toolStripSeparator;
    private System.Windows.Forms.ToolStripSeparator toolStripSeparator;
    private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog;
    private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel;
    private System.Windows.Forms.ToolStripStatusLabel tsslPlan;
    private System.Windows.Forms.ColumnHeader columnHeader;
}