Convert Image to Icon using C#
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.
Categories: .Net, .Net 3.0 / 3.5, ASP.Net, Visual Studio, Win 32 API, Windows Forms .Net, ASP.Net, C#, C#.Net, Convert Image to Icon, Drawing, Icon, Image to Icon

I usually don’t post on Blogs but ya forced me to, great info.. excellent! … I’ll add a backlink and bookmark your site.
There is obviously a lot to know about this. There are some good points here.
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
Thanks for the comments Dennis.
Thanks for the comments Tnelson.
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