C#實(shí)現(xiàn)多種圖片格式轉(zhuǎn)換的示例詳解
1.目的
實(shí)現(xiàn)多種圖片格式的相互轉(zhuǎn)換,圖片大小可自定義等。
2.知識(shí)點(diǎn)
轉(zhuǎn)換成圖標(biāo)圖像(ico)時(shí),需要獲取圖像句柄,然后根據(jù)句柄生成Ico圖像,否則生成的圖像不能作為應(yīng)用的圖標(biāo)使用。
IntPtr hwd = bitmap.GetHicon(); Icon icon = Icon.FromHandle(hwd); icon.Save(fs);
利用反射獲取系統(tǒng)可支持的圖片類型,獲取靜態(tài)屬性的值。
ImageFormat format = typeof(ImageFormat).GetProperty(comboBox1.Text).GetValue(null) as ImageFormat;
3.效果展示
4.代碼
public partial class Form1 : Form { string useExt; public Form1() { InitializeComponent(); } private void btnSelect_Click(object sender, EventArgs e) { using(OpenFileDialog ofd=new OpenFileDialog()) { ofd.Multiselect = false; if (useExt != null) { ofd.Filter = useExt; } if(ofd.ShowDialog()== DialogResult.OK) { txtFilePath.Text = ofd.FileName; } } } private void btnSave_Click(object sender, EventArgs e) { using(SaveFileDialog sfd=new SaveFileDialog()) { sfd.CheckPathExists = true; string ext=comboBox1.Text; if (comboBox1.Text.ToUpper() == "JPEG") { ext = "jpg"; } if (comboBox1.Text.ToUpper() == "ICON") { ext = "ico"; } sfd.Filter = $"*.{ext}文件|*.{ext}"; if(sfd.ShowDialog()== DialogResult.OK) { txtSavePath.Text = sfd.FileName; } } } private void button3_Click(object sender, EventArgs e) { if(string.IsNullOrEmpty(txtFilePath.Text)|| string.IsNullOrEmpty(txtSavePath.Text)) { MessageBox.Show("請先選擇文件路徑","提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } string sizeStr = comboBox2.Text; string[] wandh = sizeStr.Split('*'); double width,height; if (wandh.Length == 2) { if(double.TryParse(wandh[0],out width) && double.TryParse(wandh[1],out height)) { Image img = Image.FromFile(txtFilePath.Text); Size size ; if ((width ==1)&& (height == 1)) { size = new Size(img.Width, img.Height); } else { size = new Size((int)width, (int)height); } Bitmap bitmap = new Bitmap(img,size); ImageFormat format = typeof(ImageFormat).GetProperty(comboBox1.Text).GetValue(null) as ImageFormat; using(FileStream fs=new FileStream(txtSavePath.Text, FileMode.Create)) { if (format == ImageFormat.Icon) { IntPtr hwd = bitmap.GetHicon(); Icon icon = Icon.FromHandle(hwd); icon.Save(fs); } else { bitmap.Save(fs, format); } } MessageBox.Show("已保存至:"+txtSavePath.Text,"提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("目標(biāo)尺寸參數(shù)格式異常", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { MessageBox.Show("目標(biāo)尺寸參數(shù)數(shù)量異常", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { List<string> list = new List<string>(); //獲取所有可供轉(zhuǎn)換的類型 foreach (var item in typeof(ImageFormat).GetProperties()) { comboBox1.Items.Add(item.Name); list.Add($"*.{item.Name}文件|*.{item.Name}"); } list.Add("*.jpg文件|*.jpg"); list.Reverse(); useExt = string.Join("|", list); if (comboBox1.Items.Count > 0) comboBox1.SelectedIndex = comboBox1.Items.Count-1; comboBox2.SelectedIndex = 0; } }
到此這篇關(guān)于C#實(shí)現(xiàn)多種圖片格式轉(zhuǎn)換的示例詳解的文章就介紹到這了,更多相關(guān)C#圖片格式轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)控制線程池最大數(shù)并發(fā)線程
這篇文章主要介紹了C#實(shí)現(xiàn)控制線程池最大數(shù)并發(fā)線程的相關(guān)資料,需要的朋友可以參考下2016-07-07C# 使用Free Spire.Presentation 實(shí)現(xiàn)對PPT插入、編輯、刪除表格
小編發(fā)現(xiàn)使用.NET組件——Free Spire.Presentation,在C#中添加該產(chǎn)品DLL文件,可以簡單快速地實(shí)現(xiàn)對演示文稿的表格插入、編輯和刪除等操作,具體實(shí)現(xiàn)代碼大家參考下本文吧2017-09-09C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問題
這篇文章主要介紹了C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問題 ,文中給大家列舉通過兩種方法來判斷,需要的朋友可以參考下2018-10-10C#動(dòng)態(tài)webservice調(diào)用接口
動(dòng)態(tài)調(diào)用webservice,就可以不用添加web引用了,上線的話也只是需要改一下wsdl地址就可以了2015-05-05Unity中使用反射機(jī)制調(diào)用函數(shù)
這篇文章主要為大家詳細(xì)介紹了Unity中使用反射機(jī)制調(diào)用函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03