.NET生成ICO图标

using System;

using System.Collections.Generic;

using System.Web;

using System.Drawing;

using System.IO;



namespace EASAA.Common

{

    /// <summary>

    /// 生成Ico类

    /// </summary>

    public class IconMaker

    {



        /// <summary>

        /// 创建

        /// </summary>

        /// <param name="strfile">原图片路径,支持 *.jpg,*.png,*.bmp,*.gif</param>

        /// <param name="strIcofile">要生成的Ico全路径,如:C://upload/1/1.ico</param>

        /// <param name="_size">大小(一般传入16)</param>

        /// <returns></returns>

        public bool Create(string strfile, string strIcofile, int _size)

        {

            bool bol = false;



            try

            {

                Bitmap srcBitmap = new Bitmap(strfile);//用于保存原图



                Size size = new Size(_size, _size);//用于保存目标图标的大小           



                FileStream fs = new FileStream(strIcofile, FileMode.Create);



                Icon icon = ConvertBitmap2Ico(srcBitmap, size);



                icon.Save(fs);//将Icon保存的指定的输出



                fs.Close();



                bol = true;

            }

            catch (Exception ex)

            {



                // throw;

            }



            return bol;

        }



        /// <summary>

        /// 实现bitmap到ico的转换

        /// </summary>

        /// <param name="bitmap">原图</param>

        /// <returns>转换后的指定大小的图标</returns>

        private Icon ConvertBitmap2Ico(Bitmap bitmap, Size size)

        {

            Bitmap icoBitmap = new Bitmap(bitmap, size);//创建制定大小的原位图



            //获得原位图的图标句柄

            IntPtr hIco = icoBitmap.GetHicon();

            //从图标的指定WINDOWS句柄创建Icon

            Icon icon = Icon.FromHandle(hIco);



            return icon;

        }



    }

}

 

你可能感兴趣的:(.net)