欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)多種圖片格式轉(zhuǎn)換的示例詳解

 更新時(shí)間:2024年01月31日 09:40:14   作者:lingxiao16888  
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)多種圖片格式轉(zhuǎn)換,例如轉(zhuǎn)換成圖標(biāo)圖像ICO,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

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)文章

最新評論