技术思绪摘录旅行
很多时候我们需要将png或者jpg图片转换为ico图标,网页的icon图标需要,开发软件的时候,也需要这种图标格式,但是需要的时候再去找网站在线转换,很麻烦,所以自己写一个吧,本文将介绍最核心代码,不开源工具了

开源是程序员的基本美德

首先封装一个转换类:IcoHelper.cs


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace CarsonHelper.Until
{
    /// <summary>
    ///ico处理类
    /// </summary>
    public class IcoHelper
    {
        /// <summary>
        ///转换ico
        /// </summary>
        /// <param name="width">图标宽高</param>
        /// <param name="path">原图片地址</param>
        /// <returns>System.String.</returns>
        public static string ConvertIco(int width, string path)
        {
            try
            {
                string savepath =
                    $"{path.Substring(0, path.LastIndexOf('\\'))}\\{DateTime.Now.Ticks}_{width}X{width}.ico";
                Size size = new Size(width, width);
                //获得原始图片文件
                using (Bitmap bm = new Bitmap(path))
                {
                    using (Bitmap iconBm = new Bitmap(bm, size))
                    {
                        SaveToIcon(iconBm, Rectangle.Empty, savepath);
                    }
                }
                return savepath;
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 从pic创建Icon信息, 生成Icon的尺寸为rect
        /// </summary>
        /// <param name="pic">图片对象</param>
        /// <param name="rect">图标尺寸</param>
        /// <returns>IconInfo.</returns>
        private static IconInfo CreatIconInfo(Image pic, Rectangle rect)
        {
            rect = new Rectangle(0, 0, pic.Width, pic.Height);
            Bitmap iconBitmap = new Bitmap(rect.Width, rect.Height);
            Graphics g = Graphics.FromImage(iconBitmap);
            g.DrawImage(pic, rect, new Rectangle(0, 0, pic.Width, pic.Height), GraphicsUnit.Pixel);
            g.Dispose();
            MemoryStream memoryStream = new MemoryStream();
            iconBitmap.Save(memoryStream, ImageFormat.Bmp);
            IconInfo icon = new IconInfo();
            icon.Width = (ushort)rect.Width;
            icon.Height = (ushort)rect.Height;
            memoryStream.Position = 14;
            icon.ImageData = new byte[memoryStream.Length - memoryStream.Position];
            memoryStream.Read(icon.ImageData, 0, icon.ImageData.Length);
            byte[] height = BitConverter.GetBytes((uint)icon.Height * 2);
            icon.ImageData[8] = height[0];
            icon.ImageData[9] = height[1];
            icon.ImageData[10] = height[2];
            icon.ImageData[11] = height[3];
            icon.ImageSize = (uint)icon.ImageData.Length;
            icon.ImageOffset = 6 + (uint)(1 * 16);
            return icon;
        }


        /// <summary>
        /// 保存pic为Icon图像,尺寸rect,保存文件路径名称PathName
        /// </summary>
        /// <param name="pic">图像对象</param>
        /// <param name="rect">图标尺寸</param>
        /// <param name="pathName">保存地址</param>
        private static void SaveToIcon(Image pic, Rectangle rect, string pathName)
        {
            IconInfo iconInfo = CreatIconInfo(pic, rect);
            FileStream stream = new FileStream(pathName, FileMode.Create);
            ushort Reserved = 0;
            ushort Type = 1;
            ushort Count = 1;
            byte[] temp = BitConverter.GetBytes(Reserved);
            stream.Write(temp, 0, temp.Length);
            temp = BitConverter.GetBytes(Type);
            stream.Write(temp, 0, temp.Length);
            temp = BitConverter.GetBytes(Count);
            stream.Write(temp, 0, temp.Length);
            stream.WriteByte((byte)(iconInfo.Width < 256 ? iconInfo.Width : 0));
            stream.WriteByte((byte)(iconInfo.Height < 256 ? iconInfo.Height : 0));
            stream.WriteByte((byte)iconInfo.ColorNum);
            stream.WriteByte((byte)iconInfo.Reserved);
            temp = BitConverter.GetBytes(iconInfo.Planes);
            stream.Write(temp, 0, temp.Length);
            temp = BitConverter.GetBytes(iconInfo.PixelBit);
            stream.Write(temp, 0, temp.Length);
            temp = BitConverter.GetBytes(iconInfo.ImageSize);
            stream.Write(temp, 0, temp.Length);
            temp = BitConverter.GetBytes(iconInfo.ImageOffset);
            stream.Write(temp, 0, temp.Length);
            stream.Write(iconInfo.ImageData, 0, iconInfo.ImageData.Length);
            stream.Close();
        }

    }
    /// <summary>
    /// Icon图像信息类
    /// </summary>
    class IconInfo
    {
        public ushort Width = 16;         // 图像宽度
        public ushort Height = 16;        // 图像高度
        public ushort ColorNum = 0;       // 图像中的颜色数
        public ushort Reserved = 0;       // 保留字
        public ushort Planes = 1;       // 为目标设备说明位面数
        public ushort PixelBit = 32;    // 每个像素素所占位数
        public uint ImageSize;      // 图像字节大小
        public uint ImageOffset;    // 图形数据起点偏移位置
        public byte[] ImageData;        // 图形数据
    }

}


调用方式如下:


string savepath = IcoHelper.ConvertIco(128,"保存路径");//成功返回ico保存路径,不成功返回空


成品下载地址:https://www.lanzous.com/i8rgz4f


CarsonIT 微信扫码关注公众号 策略、创意、技术

留下您的脚步

 

最近评论

查看更多>>

精选推荐

阅读排行

友情打赏

请打开您的微信,扫一扫