一個ICON圖標的轉換程序

抽空寫了一個ICON圖標的轉換程序,支持png\jpe\bmp格式到ico的轉換。具體的程序就在下面,若是看的人多,過兩天再把思路寫一下。廢話不說,見代碼。

                                      

 this

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

namespace IconMaker
{
    public partial class Form1 : Form
    {
        Bitmap srcBitmap = null;//用於保存原圖
        Size size;//用於保存目標圖標的大小orm

        public Form1()
        {
            InitializeComponent();
        }圖片

        /// <summary>
        /// 選擇源文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void browseBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();源碼

            dlg.Filter = "圖片(*.jpg,*.png,*.bmp)|*.jpg;*.png;*.bmp";//篩選文件格式
            dlg.ValidateNames = true;//文件有效性驗證ValidateNames,驗證用戶輸入是不是一個有效的Windows文件名
            dlg.CheckFileExists = true;///驗證文件有效性
            dlg.CheckPathExists = true;//驗證路徑有效性it

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                pathTb.Text = dlg.FileName;
                srcBitmap = new Bitmap(dlg.FileName);io

                this.srcPanel.Refresh();
            }
        }class

        /// <summary>
        /// 禁止用戶在此TextBox中輸入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            //禁止用戶輸入
            e.Handled = true;
        }object

        /// <summary>
        /// 繪製原圖
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void srcPanel_Paint(object sender, PaintEventArgs e)
        {
            if (srcBitmap != null)
            {
                Graphics g = e.Graphics;date

                //原圖的繪製區域
                Rectangle rect = new Rectangle(0, 0, this.srcPanel.Width, this.srcPanel.Height);
                //在規定區域縮放繪製原圖
                g.DrawImage(srcBitmap, rect);
            }
        }

        /// <summary>
        /// 實現bitmap到ico的轉換
        /// </summary>
        /// <param name="bitmap">原圖</param>
        /// <returns>轉換後的指定大小的圖標</returns>
        private Icon ConvertBitmap2Ico(Bitmap bitmap)
        {
            Bitmap icoBitmap = new Bitmap(bitmap, size);//建立制定大小的原位圖

            //得到原位圖的圖標句柄
            IntPtr hIco = icoBitmap.GetHicon();
            //從圖標的指定WINDOWS句柄建立Icon
            Icon icon = Icon.FromHandle(hIco);

            return icon;
        }

        /// <summary>
        /// 生成並保存圖標
        /// </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();
                dlg.Filter = "圖標(*.ico)|*.ico";
                dlg.CheckPathExists = true;
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    FileStream fs = new FileStream(dlg.FileName, FileMode.Create);

                    Icon icon = ConvertBitmap2Ico(srcBitmap);

                    this.Icon = icon;

                    icon.Save(fs);//將Icon保存的指定的輸出
                    fs.Close();
                }
            }
        }

        /// <summary>        /// 選擇目標圖標的大小        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void sizeComb_SelectedIndexChanged(object sender, EventArgs e)        {            if (sizeComb.SelectedIndex == 0)            {                size = new Size(16, 16);            }            else if (sizeComb.SelectedIndex == 1)            {                size = new Size(32, 32);            }            else            {                size = new Size(48, 48);            }        }    }}點擊下載源碼

相關文章
相關標籤/搜索