C#: Convert an .Ico File to a .Bmp

Introduction

 Code to convert Icon to Bitmap either a byte array under project resources or from a physical Icon file. This is helpful when an image is needed while a developer does not have the means to convert an Icon to Bitmap using an image editor.

Example 1 form bound

Using the Bitmap class included in the System.Drawing namespace, an .ico file is converted to a .bmp file to display it in a PictureBox coded directly in a form which is not advisable as coding this way if needed in another form must be copied to the other form.

This is necessary because the Icon (.ico) format is not supported by the PictureBox control.



      using System.Drawing;
      using System.Windows.Forms;
               
      namespace WindowsFormsApplication1
      {  
                  public partial  class Form1 : Form  
                  {      
                    public Bitmap FromIconToBitmap(Icon icon)  
                    {      
                      Bitmap bmp =       new  Bitmap(icon.Width, icon.Height);  
                      using (Graphics gp = Graphics.FromImage(bmp))  
                      {      
                        gp.Clear(Color.Transparent);      
                        gp.DrawIcon(icon,       new  Rectangle(0, 0, icon.Width, icon.Height));  
                      }      
                      return bmp;  
                    }      
               
                    private void  Button1_Click(System.Object sender, System.EventArgs e)  
                    {      
                      Icon icon = WindowsFormsApplication1.Properties.Resources.Icon1;      
                      pictureBox1.Image = FromIconToBitmap(icon);      
                    }      
                  }      
      }  

Example 2 using a class

For reusability, code should be placed into a separate class or place the code into a class project. Also, placing images in a folder perhaps named Images or specifically Icons an element of dynamical select is possible plus images can be removed, modified or added.

The following class written with .NET Core 5, C# 9 provides methods to interact with icons to bitmap.

using System.Drawing;
using System.IO;
 
namespace ImageHelpers
{
    public class  Converters
    {
        /// <summary>
        /// Convert from icon to bitmap
        /// </summary>
        /// <param name="icon">Valid icon</param>
        /// <returns>Bitmap</returns>
        public static  Bitmap FromIconToBitmap(Icon icon)
        {
             
            var bitmap = new  Bitmap(icon.Width, icon.Height);
            using var gp = Graphics.FromImage(bitmap);
            gp.Clear(Color.Transparent);
            gp.DrawIcon(icon, new  Rectangle(0, 0, icon.Width, icon.Height));
             
            return bitmap;
             
        }
        /// <summary>
        /// Convert byte array to Icon
        /// </summary>
        /// <param name="bytes">Bytes to form a Icon</param>
        /// <returns>Icon</returns>
        public static  Icon BytesToIcon(byte[] bytes)
        {
            using var ms = new MemoryStream(bytes);
            return new  Icon(ms);
             
        }
        /// <summary>
        /// Read file contents and convert to BitMap
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static  Bitmap FromBytesToBitMap(string fileName)
        {
            var bytes = File.ReadAllBytes(fileName);
            var icon = BytesToIcon(bytes);
 
            return FromIconToBitmap(icon);
        }
       
    }
}

To keep form code clean for setting images to controls e.g. PictureBox, Button, Menu item the following language extension can be used.

using System.Windows.Forms;
using ImageHelpers;
 
namespace ImageConverterSample.LanguageExtensions
{
    /// <summary>
    /// Load icon from file to PictureBox
    /// </summary>
    public static  class PictureBoxExtensions
    {
        public static  void LoadIconFromFile(this PictureBox pictureBox, string fileName)
        {
            pictureBox.Image = Converters.FromBytesToBitMap(fileName);
        }
    }
}

Implementation, done by referencing the code above from a class project.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ImageConverterSample.LanguageExtensions;
using ImageHelpers;
 
namespace ImageConverterSample
{
     
#nullable enable
    public partial  class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            Shown += OnShown;
        }
 
        private void  OnShown(object? sender, EventArgs e)
        {
 
            var basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Icons");
             
            Agent1PictureBox.LoadIconFromFile(Path.Combine(basePath, "agent1.ico"));
            cSharpPictureBox.LoadIconFromFile(Path.Combine(basePath, "Csharp.ico"));
            ExcelPictureBox.LoadIconFromFile(Path.Combine(basePath, "Excel.ico"));
            SearchPictureBox.Image = Converters.FromBytesToBitMap(Path.Combine(basePath, "Search.ico"));
 
            var closeImage = Converters.FromBytesToBitMap(Path.Combine(basePath, "Close.ico"));
            CloseButton.Image = closeImage;
            ExitToolStripMenuItem.Image = closeImage;
 
        }
         
    }
}

Screenshot

Requirements

While example 1 is for .NET Framework prior to .NET Core, the code in example 2 will work either in .NET or .NET Core although minor changes are needed to use the source code for above code as C# 9 features are used.

Summary

Code provided allows a developer to use icon files in controls that don't accept icons in windows forms along with web applications since code in the second code sample resides in a separate class project.

Source code

Open Microsoft Visual Studio 2019 or Microsoft VS Code and clone the repository, perform a NuGet Restore packages, build and run.