????关于作者
众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣 !!!
专注于Android/Unity和各种游戏开发技巧,以及各种资源分享(网站、工具、素材、源码、游戏等)
有什么需要欢迎私我,交流群让学习不再孤单。
????实践过程
????效果
????代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[] path1 = null; //用于存储选择的文件列表
string path2 = ""; //用于存储保存的路径
Bitmap bt; //声明一个转换图片格式的Bitmap对象
Thread td; //声明一个线程
int Imgtype1; //声明一个变量用于标记ConvertImage方法中转换的类型
string OlePath; //声明一个变量用于存储ConvertImage方法中原始图片的路径
string path; //声明一个变量用于存储ConvertImage方法中转换后图片的保存路径
int flags; //用于标记已转换图片的数量,用于计算转换进度
private void Form2_Load(object sender, EventArgs e)
{
tscbType.SelectedIndex = 0; //设置第一个转换类型被选中
CheckForIllegalCrossThreadCalls = false; //屏蔽线程弹出的错误提示
}
private void toolStripButton3_Click(object sender, EventArgs e) //选择转换文件的按钮
{
if (openFileDialog1.ShowDialog() == DialogResult.OK) //判断是否选择文件
{
listView1.Items.Clear(); //清空listView1
string[] info = new string[7]; //存储每一行数据
FileInfo fi; //创建一个FileInfo对象,用于获取图片信息
path1 = openFileDialog1.FileNames; //获取选择的图片集合
for (int i = 0; i < path1.Length; i++) //读取集合中的内容
{
//获取图片名称
string ImgName = path1[i].Substring(path1[i].LastIndexOf("\\") + 1,
path1[i].Length - path1[i].LastIndexOf("\\") - 1);
//获取图片类型
string ImgType = ImgName.Substring(ImgName.LastIndexOf(".") + 1,
ImgName.Length - ImgName.LastIndexOf(".") - 1);
fi = new FileInfo(path1[i].ToString()); //实例化FileInfo对象
//将每一行数据第一个位置的图标添加到imageList1中
imageList1.Images.Add(ImgName, Properties.Resources.图标__23_);
info[1] = ImgName; //图片名称
info[2] = ImgType; //图片类型
info[3] = fi.LastWriteTime.ToShortDateString(); //图片最后修改日期
info[4] = path1[i].ToString(); //图片位置
info[5] = (fi.Length / 1024) + "KB"; //图片大小
info[6] = "未转换"; //图片状态
ListViewItem lvi = new ListViewItem(info, ImgName); //实例化ListViewItem对象
listView1.Items.Add(lvi); //将信息添加到listView1控件中
}
tsslFileNum.Text = "当前共有" + path1.Length.ToString() + "个文件"; //状态栏中显示图片数量
}
}
private void toolStripButton2_Click(object sender, EventArgs e) //关闭按钮
{
Application.Exit(); //退出系统
}
private void toolStripButton5_Click(object sender, EventArgs e) //清空列表的按钮
{
listView1.Items.Clear(); //清空列表
path1 = null; //清空图片的集合
tsslFileNum.Text = "当前没有文件"; //状态栏中提示
tsslPlan.Text = ""; //清空进度数字
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (path1 == null) //判断是否选择图片
{
MessageBox.Show("请选择图片!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (path2.Length == 0) //判断是否选择保存位置
{
MessageBox.Show("请选择保存位置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
flags = 1; //初始化flags变量为1,用于计算进度
toolStrip1.Enabled = false; //当转换开始时,禁用工具栏
int flag = tscbType.SelectedIndex; //判断将图片转换为何种格式
switch (flag) //根据不同的格式进行转换
{
case 0:
Imgtype1 = 0; //如果选择第一项则转换为BMP格式
td = new Thread(new ThreadStart(ConvertImage)); //通过线程调用ConvertImage方法进行转换
td.Start();
break;
case 1: //如果选择第二项则转换为JPG格式
Imgtype1 = 1;
td = new Thread(new ThreadStart(ConvertImage)); //通过线程调用ConvertImage方法进行转换
td.Start();
break;
case 2: //如果选择第三项则转换为PNG格式
Imgtype1 = 2;
td = new Thread(new ThreadStart(ConvertImage)); //通过线程调用ConvertImage方法进行转换
td.Start();
break;
case 3: //如果选择第四项则转换为GIF格式
Imgtype1 = 3;
td = new Thread(new ThreadStart(ConvertImage)); //通过线程调用ConvertImage方法进行转换
td.Start();
break;
default:
td.Abort();
break;
}
}
}
}
private void toolStripButton4_Click(object sender, EventArgs e) //选择保存路径按钮
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) //判断是否选择保存路径
{
path2 = folderBrowserDialog1.SelectedPath; //获取保存路径
}
}
private void ConvertImage()
{
flags = 1;
switch (Imgtype1)
{
case 0:
for (int i = 0; i < path1.Length; i++)
{
string ImgName = path1[i].Substring(path1[i].LastIndexOf("\\") + 1,
path1[i].Length - path1[i].LastIndexOf("\\") - 1);
ImgName = ImgName.Remove(ImgName.LastIndexOf("."));
OlePath = path1[i].ToString();
bt = new Bitmap(OlePath);
path = path2 + "\\" + ImgName + ".bmp";
bt.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
listView1.Items[flags - 1].SubItems[6].Text = "已转换";
tsslPlan.Text = "正在转换" + flags * 100 / path1.Length + "%";
if (flags == path1.Length)
{
toolStrip1.Enabled = true;
tsslPlan.Text = "图片转换全部完成";
}
flags++;
}
break;
case 1:
for (int i = 0; i < path1.Length; i++)
{
string ImgName = path1[i].Substring(path1[i].LastIndexOf("\\") + 1,
path1[i].Length - path1[i].LastIndexOf("\\") - 1);
ImgName = ImgName.Remove(ImgName.LastIndexOf("."));
OlePath = path1[i].ToString();
bt = new Bitmap(OlePath);
path = path2 + "\\" + ImgName + ".jpeg";
bt.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
listView1.Items[flags - 1].SubItems[6].Text = "已转换";
tsslPlan.Text = "正在转换" + flags * 100 / path1.Length + "%";
if (flags == path1.Length)
{
toolStrip1.Enabled = true;
tsslPlan.Text = "图片转换全部完成";
}
flags++;
}
break;
case 2:
for (int i = 0; i < path1.Length; i++)
{
string ImgName = path1[i].Substring(path1[i].LastIndexOf("\\") + 1,
path1[i].Length - path1[i].LastIndexOf("\\") - 1);
ImgName = ImgName.Remove(ImgName.LastIndexOf("."));
OlePath = path1[i].ToString();
bt = new Bitmap(OlePath);
path = path2 + "\\" + ImgName + ".png";
bt.Save(path, System.Drawing.Imaging.ImageFormat.Png);
listView1.Items[flags - 1].SubItems[6].Text = "已转换";
tsslPlan.Text = "正在转换" + flags * 100 / path1.Length + "%";
if (flags == path1.Length)
{
toolStrip1.Enabled = true;
tsslPlan.Text = "图片转换全部完成";
}
flags++;
}
break;
case 3:
for (int i = 0; i < path1.Length; i++)
{
string ImgName = path1[i].Substring(path1[i].LastIndexOf("\\") + 1,
path1[i].Length - path1[i].LastIndexOf("\\") - 1);
ImgName = ImgName.Remove(ImgName.LastIndexOf("."));
OlePath = path1[i].ToString();
bt = new Bitmap(OlePath);
path = path2 + "\\" + ImgName + ".gif";
bt.Save(path, System.Drawing.Imaging.ImageFormat.Gif);
listView1.Items[flags - 1].SubItems[6].Text = "已转换";
tsslPlan.Text = "正在转换" + flags * 100 / path1.Length + "%";
if (flags == path1.Length)
{
toolStrip1.Enabled = true;
tsslPlan.Text = "图片转换全部完成";
}
flags++;
}
break;
default:
bt.Dispose();
break;
}
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e) //关闭窗口时要关闭线程
{
if (td != null) //判断是否存在线程
{
if (td.ThreadState == ThreadState.Running) //然后判断线程是否正在运行
{
td.Abort(); //如果运行则关闭线程
}
}
}
}
partial class Form1
{
/// <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(Form1));
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.toolStripLabel1 = new System.Windows.Forms.ToolStripLabel();
this.toolStripButton3 = new System.Windows.Forms.ToolStripButton();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.toolStripButton4 = new System.Windows.Forms.ToolStripButton();
this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
this.toolStripLabel3 = new System.Windows.Forms.ToolStripLabel();
this.tscbType = new System.Windows.Forms.ToolStripComboBox();
this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
this.toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator();
this.toolStripButton5 = new System.Windows.Forms.ToolStripButton();
this.toolStripSeparator5 = new System.Windows.Forms.ToolStripSeparator();
this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.tsslFileNum = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
this.tsslPlan = new System.Windows.Forms.ToolStripStatusLabel();
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
this.columnHeader7 = new System.Windows.Forms.ColumnHeader();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.toolStrip1.SuspendLayout();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
//
// toolStrip1
//
this.toolStrip1.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripLabel1,
this.toolStripButton3,
this.toolStripSeparator1,
this.toolStripButton4,
this.toolStripSeparator2,
this.toolStripLabel3,
this.tscbType,
this.toolStripSeparator3,
this.toolStripButton1,
this.toolStripSeparator4,
this.toolStripButton5,
this.toolStripSeparator5,
this.toolStripButton2});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
this.toolStrip1.Size = new System.Drawing.Size(536, 25);
this.toolStrip1.TabIndex = 0;
this.toolStrip1.Text = "toolStrip1";
//
// toolStripLabel1
//
this.toolStripLabel1.Name = "toolStripLabel1";
this.toolStripLabel1.Size = new System.Drawing.Size(17, 22);
this.toolStripLabel1.Text = " ";
//
// toolStripButton3
//
this.toolStripButton3.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripButton3.Image = global::PictureBatchConversion.Properties.Resources.打开;
this.toolStripButton3.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButton3.Name = "toolStripButton3";
this.toolStripButton3.Size = new System.Drawing.Size(23, 22);
this.toolStripButton3.Text = "toolStripButton3";
this.toolStripButton3.ToolTipText = "选择需要转换的图片";
this.toolStripButton3.Click += new System.EventHandler(this.toolStripButton3_Click);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
//
// toolStripButton4
//
this.toolStripButton4.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripButton4.Image = global::PictureBatchConversion.Properties.Resources.图标__29_;
this.toolStripButton4.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButton4.Name = "toolStripButton4";
this.toolStripButton4.Size = new System.Drawing.Size(23, 22);
this.toolStripButton4.Text = "toolStripButton4";
this.toolStripButton4.TextImageRelation = System.Windows.Forms.TextImageRelation.Overlay;
this.toolStripButton4.ToolTipText = "选择保存位置";
this.toolStripButton4.Click += new System.EventHandler(this.toolStripButton4_Click);
//
// toolStripSeparator2
//
this.toolStripSeparator2.Name = "toolStripSeparator2";
this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25);
//
// toolStripLabel3
//
this.toolStripLabel3.Name = "toolStripLabel3";
this.toolStripLabel3.Size = new System.Drawing.Size(65, 22);
this.toolStripLabel3.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(121, 25);
//
// toolStripSeparator3
//
this.toolStripSeparator3.Name = "toolStripSeparator3";
this.toolStripSeparator3.Size = new System.Drawing.Size(6, 25);
//
// toolStripButton1
//
this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
this.toolStripButton1.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton1.Image")));
this.toolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButton1.Name = "toolStripButton1";
this.toolStripButton1.Size = new System.Drawing.Size(57, 22);
this.toolStripButton1.Text = "开始转换";
this.toolStripButton1.Click += new System.EventHandler(this.toolStripButton1_Click);
//
// toolStripSeparator4
//
this.toolStripSeparator4.Name = "toolStripSeparator4";
this.toolStripSeparator4.Size = new System.Drawing.Size(6, 25);
//
// toolStripButton5
//
this.toolStripButton5.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
this.toolStripButton5.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton5.Image")));
this.toolStripButton5.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButton5.Name = "toolStripButton5";
this.toolStripButton5.Size = new System.Drawing.Size(57, 22);
this.toolStripButton5.Text = "清空列表";
this.toolStripButton5.Click += new System.EventHandler(this.toolStripButton5_Click);
//
// toolStripSeparator5
//
this.toolStripSeparator5.Name = "toolStripSeparator5";
this.toolStripSeparator5.Size = new System.Drawing.Size(6, 25);
//
// toolStripButton2
//
this.toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
this.toolStripButton2.Font = new System.Drawing.Font("宋体", 9F);
this.toolStripButton2.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton2.Image")));
this.toolStripButton2.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButton2.Name = "toolStripButton2";
this.toolStripButton2.Size = new System.Drawing.Size(33, 22);
this.toolStripButton2.Text = "关闭";
this.toolStripButton2.Click += new System.EventHandler(this.toolStripButton2_Click);
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.tsslFileNum,
this.toolStripStatusLabel1,
this.tsslPlan});
this.statusStrip1.Location = new System.Drawing.Point(0, 321);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(536, 22);
this.statusStrip1.TabIndex = 1;
this.statusStrip1.Text = "statusStrip1";
//
// tsslFileNum
//
this.tsslFileNum.Name = "tsslFileNum";
this.tsslFileNum.Size = new System.Drawing.Size(0, 17);
//
// toolStripStatusLabel1
//
this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
this.toolStripStatusLabel1.Size = new System.Drawing.Size(23, 17);
this.toolStripStatusLabel1.Text = " ";
//
// tsslPlan
//
this.tsslPlan.Name = "tsslPlan";
this.tsslPlan.Size = new System.Drawing.Size(0, 17);
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader6,
this.columnHeader1,
this.columnHeader2,
this.columnHeader3,
this.columnHeader4,
this.columnHeader5,
this.columnHeader7});
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.listView1.FullRowSelect = true;
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(0, 25);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(536, 296);
this.listView1.SmallImageList = this.imageList1;
this.listView1.TabIndex = 2;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// columnHeader6
//
this.columnHeader6.Text = "";
this.columnHeader6.Width = 20;
//
// columnHeader1
//
this.columnHeader1.Text = "文件名";
this.columnHeader1.Width = 120;
//
// columnHeader2
//
this.columnHeader2.Text = "扩展名";
//
// columnHeader3
//
this.columnHeader3.Text = "日期";
this.columnHeader3.Width = 80;
//
// columnHeader4
//
this.columnHeader4.Text = "路径";
this.columnHeader4.Width = 120;
//
// columnHeader5
//
this.columnHeader5.Text = "大小";
this.columnHeader5.Width = 70;
//
// columnHeader7
//
this.columnHeader7.Text = "状态";
//
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// openFileDialog1
//
this.openFileDialog1.Filter = "所有图片|*.jpg;*.jpeg;*.gif;*.bmp;*.png";
this.openFileDialog1.Multiselect = true;
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(536, 343);
this.Controls.Add(this.listView1);
this.Controls.Add(this.statusStrip1);
this.Controls.Add(this.toolStrip1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form2";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "批量图片格式转换";
this.Load += new System.EventHandler(this.Form2_Load);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form2_FormClosed);
this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout();
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ToolStripLabel toolStripLabel1;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripLabel toolStripLabel3;
private System.Windows.Forms.ToolStripComboBox tscbType;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator3;
private System.Windows.Forms.ToolStripButton toolStripButton1;
private System.Windows.Forms.ToolStripButton toolStripButton2;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.ColumnHeader columnHeader4;
private System.Windows.Forms.ColumnHeader columnHeader5;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.ToolStripButton toolStripButton3;
private System.Windows.Forms.ToolStripButton toolStripButton4;
private System.Windows.Forms.ColumnHeader columnHeader6;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.ToolStripStatusLabel tsslFileNum;
private System.Windows.Forms.ToolStripButton toolStripButton5;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator4;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator5;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
private System.Windows.Forms.ToolStripStatusLabel tsslPlan;
private System.Windows.Forms.ColumnHeader columnHeader7;
}
需要的再直接Call我,直接发。
????其他
????作者:小空和小芝中的小空
????转载说明-务必注明来源:https://zhima.blog.csdn.net/
????这位道友请留步☁️,我观你气度不凡,谈吐间隐隐有王者霸气????,日后定有一番大作为????!!!旁边有点赞????收藏????今日传你,点了吧,未来你成功☀️,我分文不取,若不成功⚡️,也好回来找我。
温馨提示:点击下方卡片获取更多意想不到的资源。