Write a conversion program for the ICON to support conversion from the pngjpemp format to ico. The specific program is as follows. If there are too many people, write down your ideas in two days. For more information, see the code.
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. IO;
Namespace IconMaker
{
Public partial class Form1: Form
{
Bitmap srcBitmap = null; // used to save the source Image
Size size; // used to save the Size of the target icon
Public Form1 ()
{
InitializeComponent ();
}
/// <Summary>
/// Select the source file
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void browseBtn_Click (object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog ();
Dlg. Filter = "image (*. jpg, *. png, *. bmp) | *. jpg; *. png; *. bmp"; // Filter file format
Dlg. ValidateNames = true; // verify the file validity. Verify that the user input is a valid Windows file name.
Dlg. CheckFileExists = true; // verify the file Validity
Dlg. CheckPathExists = true; // verify the path Validity
If (dlg. ShowDialog () = DialogResult. OK)
{
PathTb. Text = dlg. FileName;
SrcBitmap = new Bitmap (dlg. FileName );
This. srcPanel. Refresh ();
}
}
/// <Summary>
/// Disable User input in this TextBox
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void textBox_KeyPress (object sender, KeyPressEventArgs e)
{
// Disable User input
E. Handled = true;
}
/// <Summary>
/// Draw the source Image
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void srcPanel_Paint (object sender, PaintEventArgs e)
{
If (srcBitmap! = Null)
{
Graphics g = e. Graphics;
// Drawing area of the source Image
Rectangle rect = new Rectangle (0, 0, this. srcPanel. Width, this. srcPanel. Height );
// Zoom in and out the original image in the specified area
G. DrawImage (srcBitmap, rect );
}
}
/// <Summary>
/// Implement conversion from bitmap to ico
/// </Summary>
/// <Param name = "bitmap"> source image </param>
/// <Returns> converted icon of the specified size </returns>
Private Icon ConvertBitmap2Ico (Bitmap bitmap)
{
Bitmap icoBitmap = new Bitmap (bitmap, size); // create the original Bitmap with the specified size
// Obtain the icon handle of the original bitmap
IntPtr hIco = icoBitmap. GetHicon ();
// Create an Icon from the specified WINDOWS handle of the Icon
Icon icon = Icon. FromHandle (hIco );
Return icon;
}
/// <Summary>
/// Generate and save the icon
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void makeBtn_Click (object sender, EventArgs e)
{
If (pathTb. Text! = "" & SizeComb. Text! = "")
{
SaveFileDialog dlg = new SaveFileDialog ();
& N