Winform中实现图片格式转换(附代码下载)
生活随笔
收集整理的這篇文章主要介紹了
Winform中实现图片格式转换(附代码下载)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
選擇一張照片并選擇保存位置和要轉換的圖片格式實現圖片格式轉換。
項目運行效果
?
注:
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
新建一個窗體頁面,然后設計頁面布局如下
?
選擇圖片按鈕點擊事件中
private void toolStripButton3_Click(object sender, EventArgs e)//選擇轉換文件的按鈕{if (openFileDialog1.ShowDialog() == DialogResult.OK)??????? //判斷是否選擇文件{listView1.Items.Clear();?????????????????????????????? //清空listView1string[] 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 toolStripButton4_Click(object sender, EventArgs e)//選擇保存路徑按鈕{if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)?? //判斷是否選擇保存路徑{path2 = folderBrowserDialog1.SelectedPath;????????????? //獲取保存路徑}}開始轉換按鈕點擊事件
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;}}}}完整窗體頁面代碼
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[] 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();?????????????????????????????? //清空listView1string[] 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();???????????????????????????????????????????? //如果運行則關閉線程}}}} }
代碼下載
https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12243042
總結
以上是生活随笔為你收集整理的Winform中实现图片格式转换(附代码下载)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis常用命令之操作Hash类型
- 下一篇: Winform中实现实时颜色拾取器显示R