Convert Image to Icon using C#
September 30th, 2009
6 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.
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
