当前位置:编程学习 > C#/ASP.NET >>

一个ICON图标的转换程序

抽空写了一个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

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,