Winform 将Ico图标转换为PNG图片

不管是做web,还是做C/S,经常会用到各种各样的功能按钮图片。然而有的时候控件要求图片格式为Ico,有时候要求是jpg、png、gif等。再加上网上的精美图标资源很多,图标的尺寸都比较统一等等因素,图标转化成png图片,然后拿去做按钮是非常不错的。

在网上找了半天,png转ico的工具很多,但是ico转png的却没有找到,所以就自己研究了下,做了个。其实做这个东西非常简单,代码也很少,这是全部代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace IconToBmp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<string> iconList = new List<string>();
        string outPath;
        string selectPath;
        /// <summary>
        /// 选择图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                iconList.Clear();
                foreach (string str in openFileDialog1.FileNames)
                {
                    iconList.Add(str);
                }
                selectPath = openFileDialog1.FileName;
                txtInput.Text = "选择了" + iconList.Count.ToString() + "个图标。";
            }
        }
        /// <summary>
        /// 选择输出文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOutput_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                outPath = folderBrowserDialog1.SelectedPath;
                txtOutput.Text = outPath;
            }
        }
        /// <summary>
        /// 转换前的数据验证
        /// </summary>
        /// <returns></returns>
        private bool Valid()
        {
            if (txtOutput.Text.Trim() == "")
            {
                MessageBox.Show("请选择图片输出位置!");
                return false;
            }
            if (iconList.Count == 0 || iconList == null)
            {
                MessageBox.Show("请选择要转化的图标!");
                return false;
            }
            return true;
        }
        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGo_Click(object sender, EventArgs e)
        {
            if (!Valid())
            {
                return;
            }
            int count = 0;
            foreach (string str in iconList)
            {
                try
                {
                    string iconName = str.Substring(str.LastIndexOf(@"\")).Replace(".ico", ".png");
                    System.Drawing.Icon icon = new Icon(str);
                    Bitmap bmp = icon.ToBitmap();
                    if (File.Exists(@outPath + iconName))//如果该文件已经存在
                    {
                        DialogResult result = MessageBox.Show(iconName.Substring(1) + "已存在,是否替换?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                        if (result == DialogResult.OK )//如果替换
                        {
                            bmp.Save(@outPath + iconName);
                            count++;
                        }
                    }
                    else
                    {
                        bmp.Save(@outPath + iconName);
                        count++;
                    }
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("遇到异常,已终止转换,已转换"+count.ToString()+"个文件,异常消息:"+ex.Message);
                    return;
                }
            }
            MessageBox.Show("共选择"+iconList.Count+"个图标,已转换" + count.ToString() + "个,"+(iconList.Count-count).ToString()+"个被跳过。");
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop;
            if (selectPath != "")
            {
                openFileDialog1.InitialDirectory = selectPath;
            }
        }
    }
}


本文是使用 B3log Solo奔放的胸毛。 进行同步发布的
posted @ 2010-08-11 14:26  LyZane  阅读(1327)  评论(0编辑  收藏  举报