Conversion Program of an ICON

Source: Internet
Author: User

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

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.