C # convert the image format (JPG, BMP, PNG, GIF) to ICO icon

Source: Internet
Author: User

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;
}
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.