您好,登錄后才能下訂單哦!
怎么在Winform中對(duì)圖片的格式進(jìn)行轉(zhuǎn)換?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
private void toolStripButton3_Click(object sender, EventArgs e)//選擇轉(zhuǎn)換文件的按鈕 { if (openFileDialog1.ShowDialog() == DialogResult.OK) //判斷是否選擇文件 { listView1.Items.Clear(); //清空l(shuí)istView1 string[] info = new string[7]; //存儲(chǔ)每一行數(shù)據(jù) FileInfo fi; //創(chuàng)建一個(gè)FileInfo對(duì)象,用于獲取圖片信息 path2 = openFileDialog1.FileNames; //獲取選擇的圖片集合 for (int i = 0; i < path2.Length; i++) //讀取集合中的內(nèi)容 { //獲取圖片名稱 string ImgName = path2[i].Substring(path2[i].LastIndexOf("\\") + 1, path2[i].Length - path2[i].LastIndexOf("\\") - 1); //獲取圖片類型 string ImgType = ImgName.Substring(ImgName.LastIndexOf(".") + 1, ImgName.Length - ImgName.LastIndexOf(".") - 1); fi = new FileInfo(path2[i].ToString()); //實(shí)例化FileInfo對(duì)象 //將每一行數(shù)據(jù)第一個(gè)位置的圖標(biāo)添加到imageList1中 imageList1.Images.Add(ImgName,Properties.Resources.圖標(biāo)__23_); info[1] = ImgName; //圖片名稱 info[2] = ImgType; //圖片類型 info[3] = fi.LastWriteTime.ToShortDateString();//圖片最后修改日期 info[4] = path2[i].ToString(); //圖片位置 info[5] = (fi.Length/1024)+"KB"; //圖片大小 info[6] = "未轉(zhuǎn)換"; //圖片狀態(tài) ListViewItem lvi = new ListViewItem(info, ImgName); //實(shí)例化ListViewItem對(duì)象 listView1.Items.Add(lvi); //將信息添加到listView1控件中 } tsslFileNum.Text = "當(dāng)前共有" + path2.Length.ToString() + "個(gè)文件";//狀態(tài)欄中顯示圖片數(shù)量 } }
選擇圖片保存位置按鈕點(diǎn)擊事件
private void toolStripButton4_Click(object sender, EventArgs e)//選擇保存路徑按鈕 { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) //判斷是否選擇保存路徑 { path3 = folderBrowserDialog1.SelectedPath; //獲取保存路徑 } }
開(kāi)始轉(zhuǎn)換按鈕點(diǎn)擊事件
private void toolStripButton1_Click(object sender, EventArgs e) { if (path2 == null) //判斷是否選擇圖片 { MessageBox.Show("請(qǐng)選擇圖片!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { if (path3.Length == 0) //判斷是否選擇保存位置 { MessageBox.Show("請(qǐng)選擇保存位置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { flags = 1; //初始化flags變量為1,用于計(jì)算進(jìn)度 toolStrip1.Enabled = false; //當(dāng)轉(zhuǎn)換開(kāi)始時(shí),禁用工具欄 int flag = tscbType.SelectedIndex; //判斷將圖片轉(zhuǎn)換為何種格式 switch (flag) //根據(jù)不同的格式進(jìn)行轉(zhuǎn)換 { case 0: Imgtype1 = 0; //如果選擇第一項(xiàng)則轉(zhuǎn)換為BMP格式 td = new Thread(new ThreadStart(ConvertImage)); //通過(guò)線程調(diào)用ConvertImage方法進(jìn)行轉(zhuǎn)換 td.Start(); break; case 1: //如果選擇第二項(xiàng)則轉(zhuǎn)換為JPG格式 Imgtype1 = 1; td = new Thread(new ThreadStart(ConvertImage));//通過(guò)線程調(diào)用ConvertImage方法進(jìn)行轉(zhuǎn)換 td.Start(); break; case 2: //如果選擇第三項(xiàng)則轉(zhuǎn)換為PNG格式 Imgtype1 = 2; td = new Thread(new ThreadStart(ConvertImage));//通過(guò)線程調(diào)用ConvertImage方法進(jìn)行轉(zhuǎn)換 td.Start(); break; case 3: //如果選擇第四項(xiàng)則轉(zhuǎn)換為GIF格式 Imgtype1 = 3; td = new Thread(new ThreadStart(ConvertImage));//通過(guò)線程調(diào)用ConvertImage方法進(jìn)行轉(zhuǎn)換 td.Start(); break; default: td.Abort(); break; } } } }
完整窗體頁(yè)面代碼
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; using System.Threading; namespace PictureBatchConversion { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string[] path2=null; //用于存儲(chǔ)選擇的文件列表 string path3=""; //用于存儲(chǔ)保存的路徑 Bitmap bt; //聲明一個(gè)轉(zhuǎn)換圖片格式的Bitmap對(duì)象 Thread td; //聲明一個(gè)線程 int Imgtype1; //聲明一個(gè)變量用于標(biāo)記ConvertImage方法中轉(zhuǎn)換的類型 string OlePath; //聲明一個(gè)變量用于存儲(chǔ)ConvertImage方法中原始圖片的路徑 string path; //聲明一個(gè)變量用于存儲(chǔ)ConvertImage方法中轉(zhuǎn)換后圖片的保存路徑 int flags; //用于標(biāo)記已轉(zhuǎn)換圖片的數(shù)量,用于計(jì)算轉(zhuǎn)換進(jìn)度 private void Form2_Load(object sender, EventArgs e) { tscbType.SelectedIndex = 0; //設(shè)置第一個(gè)轉(zhuǎn)換類型被選中 CheckForIllegalCrossThreadCalls = false;//屏蔽線程彈出的錯(cuò)誤提示 } private void toolStripButton3_Click(object sender, EventArgs e)//選擇轉(zhuǎn)換文件的按鈕 { if (openFileDialog1.ShowDialog() == DialogResult.OK) //判斷是否選擇文件 { listView1.Items.Clear(); //清空l(shuí)istView1 string[] info = new string[7]; //存儲(chǔ)每一行數(shù)據(jù) FileInfo fi; //創(chuàng)建一個(gè)FileInfo對(duì)象,用于獲取圖片信息 path2 = openFileDialog1.FileNames; //獲取選擇的圖片集合 for (int i = 0; i < path2.Length; i++) //讀取集合中的內(nèi)容 { //獲取圖片名稱 string ImgName = path2[i].Substring(path2[i].LastIndexOf("\\") + 1, path2[i].Length - path2[i].LastIndexOf("\\") - 1); //獲取圖片類型 string ImgType = ImgName.Substring(ImgName.LastIndexOf(".") + 1, ImgName.Length - ImgName.LastIndexOf(".") - 1); fi = new FileInfo(path2[i].ToString()); //實(shí)例化FileInfo對(duì)象 //將每一行數(shù)據(jù)第一個(gè)位置的圖標(biāo)添加到imageList1中 imageList1.Images.Add(ImgName,Properties.Resources.圖標(biāo)__23_); info[1] = ImgName; //圖片名稱 info[2] = ImgType; //圖片類型 info[3] = fi.LastWriteTime.ToShortDateString();//圖片最后修改日期 info[4] = path2[i].ToString(); //圖片位置 info[5] = (fi.Length/1024)+"KB"; //圖片大小 info[6] = "未轉(zhuǎn)換"; //圖片狀態(tài) ListViewItem lvi = new ListViewItem(info, ImgName); //實(shí)例化ListViewItem對(duì)象 listView1.Items.Add(lvi); //將信息添加到listView1控件中 } tsslFileNum.Text = "當(dāng)前共有" + path2.Length.ToString() + "個(gè)文件";//狀態(tài)欄中顯示圖片數(shù)量 } } private void toolStripButton2_Click(object sender, EventArgs e) //關(guān)閉按鈕 { Application.Exit(); //退出系統(tǒng) } private void toolStripButton5_Click(object sender, EventArgs e) //清空列表的按鈕 { listView1.Items.Clear(); //清空列表 path2 = null; //清空?qǐng)D片的集合 tsslFileNum.Text = "當(dāng)前沒(méi)有文件"; //狀態(tài)欄中提示 tsslPlan.Text = ""; //清空進(jìn)度數(shù)字 } private void toolStripButton1_Click(object sender, EventArgs e) { if (path2 == null) //判斷是否選擇圖片 { MessageBox.Show("請(qǐng)選擇圖片!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { if (path3.Length == 0) //判斷是否選擇保存位置 { MessageBox.Show("請(qǐng)選擇保存位置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { flags = 1; //初始化flags變量為1,用于計(jì)算進(jìn)度 toolStrip1.Enabled = false; //當(dāng)轉(zhuǎn)換開(kāi)始時(shí),禁用工具欄 int flag = tscbType.SelectedIndex; //判斷將圖片轉(zhuǎn)換為何種格式 switch (flag) //根據(jù)不同的格式進(jìn)行轉(zhuǎn)換 { case 0: Imgtype1 = 0; //如果選擇第一項(xiàng)則轉(zhuǎn)換為BMP格式 td = new Thread(new ThreadStart(ConvertImage)); //通過(guò)線程調(diào)用ConvertImage方法進(jìn)行轉(zhuǎn)換 td.Start(); break; case 1: //如果選擇第二項(xiàng)則轉(zhuǎn)換為JPG格式 Imgtype1 = 1; td = new Thread(new ThreadStart(ConvertImage));//通過(guò)線程調(diào)用ConvertImage方法進(jìn)行轉(zhuǎn)換 td.Start(); break; case 2: //如果選擇第三項(xiàng)則轉(zhuǎn)換為PNG格式 Imgtype1 = 2; td = new Thread(new ThreadStart(ConvertImage));//通過(guò)線程調(diào)用ConvertImage方法進(jìn)行轉(zhuǎn)換 td.Start(); break; case 3: //如果選擇第四項(xiàng)則轉(zhuǎn)換為GIF格式 Imgtype1 = 3; td = new Thread(new ThreadStart(ConvertImage));//通過(guò)線程調(diào)用ConvertImage方法進(jìn)行轉(zhuǎn)換 td.Start(); break; default: td.Abort(); break; } } } } private void toolStripButton4_Click(object sender, EventArgs e)//選擇保存路徑按鈕 { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) //判斷是否選擇保存路徑 { path3 = folderBrowserDialog1.SelectedPath; //獲取保存路徑 } } private void ConvertImage() { flags = 1; switch (Imgtype1) { case 0: for (int i = 0; i < path2.Length; i++) { string ImgName = path2[i].Substring(path2[i].LastIndexOf("\\") + 1, path2[i].Length - path2[i].LastIndexOf("\\") - 1); ImgName = ImgName.Remove(ImgName.LastIndexOf(".")); OlePath = path2[i].ToString(); bt = new Bitmap(OlePath); path = path3 + "\\" + ImgName + ".bmp"; bt.Save(path, System.Drawing.Imaging.ImageFormat.Bmp); listView1.Items[flags - 1].SubItems[6].Text = "已轉(zhuǎn)換"; tsslPlan.Text = "正在轉(zhuǎn)換"+flags*100/path2.Length+"%"; if (flags == path2.Length) { toolStrip1.Enabled = true; tsslPlan.Text = "圖片轉(zhuǎn)換全部完成"; } flags++; } break; case 1: for (int i = 0; i < path2.Length; i++) { string ImgName = path2[i].Substring(path2[i].LastIndexOf("\\") + 1, path2[i].Length - path2[i].LastIndexOf("\\") - 1); ImgName = ImgName.Remove(ImgName.LastIndexOf(".")); OlePath = path2[i].ToString(); bt = new Bitmap(OlePath); path = path3 + "\\" + ImgName + ".jpeg"; bt.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); listView1.Items[flags - 1].SubItems[6].Text = "已轉(zhuǎn)換"; tsslPlan.Text = "正在轉(zhuǎn)換" + flags * 100 / path2.Length + "%"; if (flags == path2.Length) { toolStrip1.Enabled = true; tsslPlan.Text = "圖片轉(zhuǎn)換全部完成"; } flags++; } break; case 2: for (int i = 0; i < path2.Length; i++) { string ImgName = path2[i].Substring(path2[i].LastIndexOf("\\") + 1, path2[i].Length - path2[i].LastIndexOf("\\") - 1); ImgName = ImgName.Remove(ImgName.LastIndexOf(".")); OlePath = path2[i].ToString(); bt = new Bitmap(OlePath); path = path3 + "\\" + ImgName + ".png"; bt.Save(path, System.Drawing.Imaging.ImageFormat.Png); listView1.Items[flags - 1].SubItems[6].Text = "已轉(zhuǎn)換"; tsslPlan.Text = "正在轉(zhuǎn)換" + flags * 100 / path2.Length + "%"; if (flags == path2.Length) { toolStrip1.Enabled = true; tsslPlan.Text = "圖片轉(zhuǎn)換全部完成"; } flags++; } break; case 3: for (int i = 0; i < path2.Length; i++) { string ImgName = path2[i].Substring(path2[i].LastIndexOf("\\") + 1, path2[i].Length - path2[i].LastIndexOf("\\") - 1); ImgName = ImgName.Remove(ImgName.LastIndexOf(".")); OlePath = path2[i].ToString(); bt = new Bitmap(OlePath); path = path3 + "\\" + ImgName + ".gif"; bt.Save(path, System.Drawing.Imaging.ImageFormat.Gif); listView1.Items[flags - 1].SubItems[6].Text = "已轉(zhuǎn)換"; tsslPlan.Text = "正在轉(zhuǎn)換" + flags * 100 / path2.Length + "%"; if (flags == path2.Length) { toolStrip1.Enabled = true; tsslPlan.Text = "圖片轉(zhuǎn)換全部完成"; } flags++; } break; default: bt.Dispose(); break; } } private void Form2_FormClosed(object sender, FormClosedEventArgs e)//關(guān)閉窗口時(shí)要關(guān)閉線程 { if (td != null) //判斷是否存在線程 { if (td.ThreadState == ThreadState.Running) //然后判斷線程是否正在運(yùn)行 { td.Abort(); //如果運(yùn)行則關(guān)閉線程 } } } } }
看完上述內(nèi)容,你們掌握怎么在Winform中對(duì)圖片的格式進(jìn)行轉(zhuǎn)換的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。