抽空寫了一個ICON圖標的轉換程序,支持pngjpemp格式到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();
&n