抽空寫了一個ICON表徵圖的轉換程式

來源:互聯網
上載者:User
     抽空寫了一個ICON表徵圖的轉換程式,支援png\jpe\bmp格式到ico的轉換。具體的程式就在下面,如果看的人多,過兩天再把思路寫一下。廢話不說,見代碼。

                                      

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;//用於儲存原圖
        Size size;//用於儲存靶心圖表標的大小

        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;//驗證路徑有效性

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

                this.srcPanel.Refresh();
            }
        }

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

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

                //原圖的繪製地區
                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);
            }
        }
    }
}

            檔案下載:       Icon轉換                       

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.