目录
实践过程
效果

代码
public partial class Form : Form
{
public Form()
{
InitializeComponent();
}
string[] path = null;
string path = "";
Bitmap bt;
Thread td;
int Imgtype;
string OlePath;
string path;
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();
string[] info = new string[];
FileInfo fi;
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());
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);
listView.Items.Add(lvi);
}
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 =;
toolStrip.Enabled = false;
int flag = tscbType.SelectedIndex;
switch (flag)
{
case:
Imgtype = 0;
td = new Thread(new ThreadStart(ConvertImage));
td.Start();
break;
case:
Imgtype = 1;
td = new Thread(new ThreadStart(ConvertImage));
td.Start();
break;
case:
Imgtype = 2;
td = new Thread(new ThreadStart(ConvertImage));
td.Start();
break;
case:
Imgtype = 3;
td = new Thread(new ThreadStart(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
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
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();
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";
this.toolStripLabel.Name = "toolStripLabel1";
this.toolStripLabel.Size = new System.Drawing.Size(17, 22);
this.toolStripLabel.Text = " ";
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);
this.toolStripSeparator.Name = "toolStripSeparator1";
this.toolStripSeparator.Size = new System.Drawing.Size(6, 25);
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);
this.toolStripSeparator.Name = "toolStripSeparator2";
this.toolStripSeparator.Size = new System.Drawing.Size(6, 25);
this.toolStripLabel.Name = "toolStripLabel3";
this.toolStripLabel.Size = new System.Drawing.Size(65, 22);
this.toolStripLabel.Text = "转换格式:";
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);
this.toolStripSeparator.Name = "toolStripSeparator3";
this.toolStripSeparator.Size = new System.Drawing.Size(6, 25);
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);
this.toolStripSeparator.Name = "toolStripSeparator4";
this.toolStripSeparator.Size = new System.Drawing.Size(6, 25);
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);
this.toolStripSeparator.Name = "toolStripSeparator5";
this.toolStripSeparator.Size = new System.Drawing.Size(6, 25);
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);
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";
this.tsslFileNum.Name = "tsslFileNum";
this.tsslFileNum.Size = new System.Drawing.Size(, 17);
this.toolStripStatusLabel.Name = "toolStripStatusLabel1";
this.toolStripStatusLabel.Size = new System.Drawing.Size(23, 17);
this.toolStripStatusLabel.Text = " ";
this.tsslPlan.Name = "tsslPlan";
this.tsslPlan.Size = new System.Drawing.Size(, 17);
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;
this.columnHeader.Text = "";
this.columnHeader.Width = 20;
this.columnHeader.Text = "文件名";
this.columnHeader.Width = 120;
this.columnHeader.Text = "扩展名";
this.columnHeader.Text = "日期";
this.columnHeader.Width = 80;
this.columnHeader.Text = "路径";
this.columnHeader.Width = 120;
this.columnHeader.Text = "大小";
this.columnHeader.Width = 70;
this.columnHeader.Text = "状态";
this.imageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
this.imageList.ImageSize = new System.Drawing.Size(16, 16);
this.imageList.TransparentColor = System.Drawing.Color.Transparent;
this.openFileDialog.Filter = "所有图片|*.jpg;*.jpeg;*.gif;*.bmp;*.png";
this.openFileDialog.Multiselect = true;
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;
}