C ා image conversion ICO tool

I used to like to use online method to convert pictures into ICO files. If there is no net, there is no way. So I made a software tool. Convert BMP,JPG,PNP,GIF.ICO to specified size ICO file.

1. Document format judgment

The format is judged by binary file header instead of file extension.

        /// <summary>
        ///Read the file header to determine whether it is a picture format, and output it to typename
        /// </summary>
        ///< param name = "typename" > picture format < / param >
        ///< param name = "filepath" > file path < / param >
        ///< returns > whether it is the specified image format < / returns >
        private bool IsPicture(out string typename, string filePath)
        {
            try
            {
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                BinaryReader reader = new BinaryReader(fs);
                string fileClass = "";
                byte[] cb = new byte[6];
                cb = reader.ReadBytes(6);
                if (cb[0] == 0)
                {
                    foreach (var bs in cb)
                    {
                        fileClass += bs.ToString();
                    }
                }
                else {
                    for (int i = 0; i < 2;i++ )
                    {
                        fileClass += cb[i].ToString();
                    }
                }
                reader.Close();
                fs.Close();
                if (
                    fileClass == FILEHEAD_BMP
                    || fileClass == FILEHEAD_JPG
                    || fileClass == FILEHEAD_PNG
                    || fileClass == FILEHEAD_GIF
                    || fileClass == FILEHEAD_ICO
                    )
                {
                    typename = fileClass;
                    return true;
                }
                else
                {
                    typename = "";
                    return false;
                }
            }
            catch
            {
                typename = "";
                return false;
            }
        }


2. Convert Image to Icon

I also find this code from the Internet, and I don't know who the original is, because many articles have it.

        /// <summary>
        ///Convert Image to Icon
        /// </summary>
        ///< param name = "Image" > Image object to convert to icon < / param >
        public static Icon ConvertToIcon(Image image )
        {
            if (image == null)
            {
                 return null; 
            }

            using (MemoryStream msImg = new MemoryStream()
                              , msIco = new MemoryStream())
            {
                image.Save(msImg, ImageFormat.Png);

                using (var bin = new BinaryWriter(msIco))
                {
                    //Write icon head
                    bin.Write((short)0);           //0-1 reserved
                    bin.Write((short)1);           //2-3 document type. 1 = icon, 2 = cursor
                    bin.Write((short)1);           //4-5 number of images (icons can contain multiple images)

                    bin.Write((byte)image.Width);  //6 Icon width
                    bin.Write((byte)image.Height); //7 Icon height
                    bin.Write((byte)0);            //8 color number (if pixel bit depth >=8, fill in 0. It is obvious that the minimum number of colors reaching 8bpp is 256, and byte is not enough.)
                    bin.Write((byte)0);            //9 reserved. Must be 0
                    bin.Write((short)0);           //10-11 palette
                    bin.Write((short)32);          //12-13 bit depth
                    bin.Write((int)msImg.Length);  //14-17 bitmap data size
                    bin.Write(22);                 //18-21 bitmap data start byte

                    //Write image data
                    bin.Write(msImg.ToArray());

                    bin.Flush();
                    bin.Seek(0, SeekOrigin.Begin);
                    return new Icon(msIco);
                }
            }
        }
3. Interface

Drag several buttons, label,panel, set the main window to allow dragging files, several events, etc. The tool class often needs to set the top function, so it adds a set top switch. The interface is as follows:



Set the size of the ICO to be output, then drag a picture into the form or open it through the button, you can convert it and save it as ICO.


ps: there is a problem. If the file name is too long, the status bar will not be displayed! It's not just a part of it. It's amazing.



Posted on Mon, 04 May 2020 04:40:53 -0400 by ThisIsMyName