抽空寫了一個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轉換