Convert Image to Icon using C#
By Anuraj P on September 30th, 2009 . 6 Comments .
.Net, .Net 3.0 / 3.5, ASP.Net, Visual Studio, Win 32 API, Windows Forms Tags: .Net, ASP.Net, C#, C#.Net, Convert Image to Icon, Drawing, Icon, Image to Icon
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.
Search
categories
- Code coverage (1)
- Uncategorized (4)
- Version Control (8)
- Unit Testing (14)
- SQL Server (16)
- sharepoint (18)
- Windows 7 (19)
- User Group Activities (27)
- Javascript (33)
- ASP.Net (60)
- Windows Forms (62)
- Visual Studio (67)
- Miscellaneous (69)
- .Net 3.0 / 3.5 (149)
- .Net (272)
- Office Interoperability (4)
- ASP.Net MVC (5)
- WCF (8)
- Silverlight (15)
- Win 32 API (17)
- WPF (29)
- .Net 4.0 (62)
Copyright © 2007-2012 dotnet thoughts
Powered by GoIsha.com.