Friday, December 12, 2008

Create Icons from Bitmap Files


Create Icons from Bitmap Files

The following code generates standard icon files from Bitmaps.

Note: To create standard icons, compatible with any graphics applications, first, you may scale your Bitmap files into a size of (x*32)*(x*32); e.g., 32*32, 64*64, 96*96, 128*128* ...


////////////////////////////////////////////////////
////////////////////////////////////////////////////
////////////////////////////////////////////////////

/////
///// Description: Source code to generate standard
///// icons (for Windows, Web, etc.)
///// Author: G. R. Roosta

///// License: Free To Use (No Restriction)

/////
////////////////////////////////////////////////////
////////////////////////////////////////////////////
////////////////////////////////////////////////////

using System.IO;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Brainvol.IntelliPhoto.IntelliIcons.CreateIcon
{
public partial class IntelliIcon : Form
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
extern static bool DestroyIcon(IntPtr handle);
public IntelliIcon()
{
InitializeComponent();
}
private void IntelliIcon_Load(object sender, EventArgs e)
{
}
private void btnCreateIcon_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "C:\\";
openFileDialog1.Filter = "Bitmap(*.bmp)*.bmp";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string sourceBitmap = openFileDialog1.FileName;
//
// Create a Bitmap object from the selected Bitmap file.
//

Bitmap bmp = new Bitmap(sourceBitmap);
//
// Get an Hicon for myBitmap.
//
IntPtr Hicon = bmp.GetHicon();
//
// Create a new icon from the handle.
//

Icon newIcon = Icon.FromHandle(Hicon);
//
// Write Icon to File Stream.
//
string destFileName = sourceBitmap.Substring(0, sourceBitmap.Length - 3) + "ico";
FileStream fs = new FileStream(destFileName, FileMode.OpenOrCreate);
newIcon.Save(fs);
fs.Close();
}
}
}
}