Using System;
Using System. Drawing;
Using System. Windows. Forms;
Using System. IO;
Namespace ICOTest
{
Public partial class Form1: Form
{
String FileName = "C :\";
Public Form1 ()
{
InitializeComponent ();
}
// Core code
Private void button#click (object sender, EventArgs e)
{
Size size = new Size (32, 32 );
// Obtain the original image file
Using (Bitmap bm = new Bitmap (FileName ))
{
// Scale down an existing image to obtain an appropriate ICO File
Using (Bitmap iconBm = new Bitmap (bm, size ))
{
// If it is a windows call, you can simply use the following line of code.
// This Code cannot be called in a web program, and a security exception is thrown.
Using (Icon = icon. FromHandle (iconBm. GetHicon ()))
{
String Name _ = "C: \ ICO \" + DateTime. Now. Ticks. ToString () + ". ico ";
Using (Stream stream = new System. IO. FileStream (Name _, System. IO. FileMode. Create ))
{
Icon. Save (stream );
MessageBox. Show ("converted successfully. Path: "+ Name _);
}
}
}
}
}
Private void button2_Click (object sender, EventArgs e)
{
OpenFileDialog1.Filter = "JPG | *. jpg | BMP | *. bmp | PNG | *. png | GIF | *. gif ";
DialogResult D = openFileDialog1.ShowDialog ();
If (D = DialogResult. OK)
{
FileName = this. openFileDialog1.FileName. Trim ();
This. textBox1.Text = FileName;
}
}
}
}