Document Solutions for Imaging
Features / Work with ICO files
In This Topic
    Work with ICO files
    In This Topic

    DsImaging supports ICO file format which is a widely used image file format for computer icons. It stores a collection of small images of different sizes and color sets. The images can be saved in ICO file format by using GcIco class. You can work with different frames of an ICO file by using the methods of IcoFrame class.

    You can also load and save icons in various encodings by using IcoFrameEncoding enumeration which sets the encoding of an ICO frame image. For example, a frame can be stored in PNG format or as indexed image with color palette and transparency mask.


    Create an ICO File

    DsImaging lets you create the frames of an ICO image file from scratch or load from an existing ICO file. These frames can also be converted to GcBitmap or created from existing GcBitmap instances. The whole collection can then be saved to an ICO file. The frames in a multiframe ICO image file can be appended, removed, modified, or reordered.

    To create an ICO file from a PNG image:

    1. Instantiate GcBitmap class and load the PNG file in GcBitmap instance.
    2. Initialize GcIco class and add the bitmap instance as an ICO frame.    
    3. Save the ICO image file using Save method.
    C#
    Copy Code
    //Load a png file
    var srcPath = System.IO.Path.Combine("gcd-hex-logo.png");
    var srcBmp = new GcBitmap(srcPath);
               
    //Resize the image
    var bmp256 = srcBmp.Resize(256, 256);
    
    var ico = new GcIco();
    //Add ico file frame 
    ico.Frames.Add(new IcoFrame(bmp256, IcoFrameEncoding.Png));
               
    //Save ico image file
    ico.Save("GcDocs.ico");
    

    Read Images from ICO File

    You can load the image data in ICO format from a file, stream, or an array of bytes. It can then be saved to a stream or file. The GcIco class must be disposed off after use, to prevent memory loss in image frames. Also, dispose off any removed frames from the collection.

    To read a multiframe ICO file and save its frames as separate PNG images:

    1. Load an ICO file by instantiating the GcIco class.
    2. Convert the ICO frames to GcBitmap and save them as separate PNG files.
    C#
    Copy Code
    //Load an ico file
    using (var ico = new GcIco("Windows.ico"))
    {
    for (int i = 0; i < ico.Frames.Count; i++)
    {
        //Save png file for every ico frame
        using (var bmp = ico.Frames[i].ToGcBitmap())        
        {        
            bmp.SaveAsPng($"image{i}.png");
        }
    }
    }
    


    Limitations