Code for Converting an Image to Icon

This code will convert an Image to Icon. It is using System.Drawing namespace for Image manipulation and System.IO namespace of generating filename and writing file content.


///
/// Converts an Image to Icon - Supports Image Size upto 128x128
///

/// Filename of the Image to convert
private void Image2Icon(string fileName)
{
//Creats filename with ico extention
string newFileName = Path.ChangeExtension(fileName, ".ico");
//Loading the File to the Bitmap
using (Bitmap bitMap = Image.FromFile(fileName,true) as Bitmap)
{
using (Icon icon = Icon.FromHandle(bitMap.GetHicon()))
{
//Creating the Icon file and Writing the content
using (Stream stream = File.Create(newFileName))
{
icon.Save(stream);
}
}
}
}


Reference: http://anuraj.wordpress.com/2009/09/30/convert-image-to-icon-using-c/


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: