How to Add an Icon to a Console Application (C#)

  1. Add references to these 4 assemblies to your console application: PresentationCore, System.Drawing, System.Xaml and WindowsBase.
    Searching for the Presentation Core assembly
    This is how you add the PresentationCore assembly, just press OK!
  2. The file(s) containing these classes must include the following using directives. They are the ones present in Program.cs after creating a console application, plus the ones I added via Resolve:
    CSHARP
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Media.Imaging;
  3. Now let’s create a few classes. They are excerpts from my helper classes, keeping only the methods used in this example:
    CSHARP
    class BitmapImageHelper
    {
        public static BitmapImage MsAppx(string relativePath)
        {
            BitmapImage bs = new BitmapImage(new Uri(ImageHelper.protocol + relativePath, UriKind.Absolute));
            return bs;
        }
    }
    class ImageHelper
    {
        public static string protocol = "pack://application:,,,";
        public static void SetAssemblyNameForWpfApps(string assemblyName)
        {
            protocol += "/" + assemblyName + ";component/";
        }
        public static void RegisterPackProtocol()
        {
            if (!UriParser.IsKnownScheme("pack"))
            new System.Windows.Application();
        }
    }
    class Pictures
    {
        public static Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage)
        {
            using (MemoryStream outStream = new MemoryStream())
            {
                BitmapEncoder encoder = new BmpBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
                encoder.Save(outStream);
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);
                // return bitmap; <-- leads to problems, stream is closed/closing ...
                return new Bitmap(bitmap);
            }
        }
    }
    class NativeMethods
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool SetConsoleIcon(IntPtr hIcon);
    }
  4. Finally, we write the code in Main() that adds the icon to the console application:
    CSHARP
    Console.Title = "Console application with the MS logo";
    //-tempConsoleWithoutSwf is the assembly name of the calling code (in case these helper classes live in a different assembly)!
    ImageHelper.SetAssemblyNameForWpfApps("tempConsoleWithoutSwf");
    ImageHelper.RegisterPackProtocol();
    BitmapImage bi = BitmapImageHelper.MsAppx("LogoMicrosoft.png");
    Bitmap logoBitmap = Pictures.BitmapImage2Bitmap(bi);
    NativeMethods.SetConsoleIcon(logoBitmap.GetHicon());
    Console.WriteLine("The program has finished, press Enter to exit.");
    Console.ReadLine();
  5. All that remains is to add the image file LogoMicrosoft.png to the project root, ideally in a square size (here it was 50x50), and change its Build action from Content to Resource. The result then looks something like this:
    Console application with an icon
Categories

Leave a Reply