Home > .Net, .Net 3.0 / 3.5, ASP.Net, Visual Studio, Win 32 API, Windows Forms > Convert Image to Icon using C#

Convert Image to Icon using C#

September 30th, 2009 Anuraj P Leave a comment Go to comments

Sometimes we will get nice Images from Web as Icons. But we can’t use these Images as application icons in .Net, because the .Net supports *.ico(Icon) format only. This code will convert an Image to Icon using C#. It is written .Net Framework 3.5, but it should work in .Net 2.0. It supports Images upto size 128×128. And supports various image formats(*.jpg,*.gif, *.png. *.bmp).

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

string fileName, newFileName;
fileName = "C:\Sample.jpg";
newFileName = Path.ChangeExtension(fileName, ".ico");
using (Bitmap bitmap = Image.FromFile(fileName, true) as Bitmap)
{
    using (Icon icon = Icon.FromHandle(bitmap.GetHicon()))
    {
        using (Stream imageFile = File.Create(newFileName))
        {
            icon.Save(imageFile);
            Console.WriteLine("Converted - {0}", newFileName);
        }
    }
}

Code it pretty self explanatory. Let me know if you have faced any issues.

  1. September 30th, 2009 at 17:07 | #1

    I usually don’t post on Blogs but ya forced me to, great info.. excellent! … I’ll add a backlink and bookmark your site.

  2. September 30th, 2009 at 22:37 | #2

    There is obviously a lot to know about this. There are some good points here.

  3. October 5th, 2009 at 06:58 | #3

    Hi anu,
    Nice work.i tried ur code for converting image to icon file.its working fine.but its not that much of clarity.for this i need to do anything else.

    Rgrds
    Kannabiran

  4. October 1st, 2009 at 14:37 | #4

    Thanks for the comments Dennis.

  5. October 1st, 2009 at 14:37 | #5

    Thanks for the comments Tnelson.

  6. October 5th, 2009 at 08:08 | #6

    Hi
    Thanks for the comments. It is know issue. I am trying to fix it. I will update it once I fix it.

    thanks
    Anuraj

  1. No trackbacks yet.