asp.net 图标提取以及图标转换的实例代码

所属分类: 网络编程 / ASP.NET 阅读数: 1091
收藏 0 赞 0 分享

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Resources;
using System.Reflection;
using System.Runtime.InteropServices;


namespace newExtIcon
{
    public partial class MainForm : Form
    {   
        public Assembly asm = Assembly.GetExecutingAssembly();
        public MainForm()
        {
            InitializeComponent();       
        }

        void MainForm_Load(object sender ,EventArgs e)
        {
            lbl_display.Text ="No Work !";   

        }

        void btnExtractClick(object sender, EventArgs e)
        {
            Random rd = new Random();
            openFileDialog1.Filter="应用程序|*.dll;*.exe|所有程序|*.*";
            if(openFileDialog1.ShowDialog()== DialogResult.OK)
            {
                Icon icon = Icon.ExtractAssociatedIcon(openFileDialog1.FileName);
                FileStream fileStream = new FileStream("d:\\"+rd.Next(1,100000)+".ico",FileMode.Create);
                icon.Save(fileStream);
                fileStream.Close();       
                lbl_display.Text="Work Done!";
            }
            else
            {
                return ;
            }
        }

        void BtnChangeClick(object sender, EventArgs e)
        {
            Size sz = new Size(32,32);
            Random rd = new Random();
            openFileDialog1.Filter="图片|*.jpg;*.png;*.bmp|所有程序|*.*";
            //openFileDialog1.Filter="图片|*.jpg;*.png;*.bmp|应用程序|*.dll;*.exe";
            if(openFileDialog1.ShowDialog()== DialogResult.OK)
            {
                using(Bitmap bm = new Bitmap(openFileDialog1.FileName))
                {
                    using(Bitmap IconBM = new Bitmap(bm,sz))
                    {
                        using(Icon icon_t = Icon.FromHandle(IconBM.GetHicon()))
                        {
                            FileStream fileStream = new FileStream("d:\\"+rd.Next(1,100000)+".ico",FileMode.Create);
                            icon_t.Save(fileStream);
                            fileStream.Close();   
                            lbl_display.Text="Work Done!";        
                        }
                    }
                }
            }
            else
            {
                return ;
            }

        }
        void Pic_Click(object sender, System.EventArgs e)
        {
            int int_index = Convert.ToInt32(((PictureBox)sender).Tag) ;
            switch(int_index)
            {
                case 1:
                    this.Close();
                    Application.Exit();
                    break;
                case 2:
                    WindowState = FormWindowState.Minimized;
                    break;
                default :
                    break;

            }

        }

        void Pic_Hover(object sender,EventArgs e)
        {
            string  name_1 = "btn_close_hightligth.png";
            string name_2 ="btn_mini_highlight.png";
            Bitmap bm_1 = new Bitmap(asm.GetManifestResourceStream(name_1));
            Bitmap bm_2 = new Bitmap(asm.GetManifestResourceStream(name_2));
            int int_index = Convert.ToInt32(((PictureBox)sender).Tag) ;
            switch(int_index)
            {
                case 1:
                    pictureBox1.BackgroundImage=bm_1;
                    break;
                case 2:
                    pictureBox2 .BackgroundImage = bm_2;
                    break;
                default :
                    break;

            }
        }
        void Pic_Leave(object sender,EventArgs e)
        {
            string  name_1 = "btn_close_down.png";
            string  name_2 ="btn_mini_down.png";
            Bitmap bm_1 = new Bitmap(asm.GetManifestResourceStream(name_1));
            Bitmap bm_2 = new Bitmap(asm.GetManifestResourceStream(name_2));
            //GetMainfestResourceStream获取的是流
            int int_index = Convert.ToInt32(((PictureBox)sender).Tag) ;
            switch(int_index)
            {
                case 1:
                    pictureBox1.BackgroundImage=bm_1;
                    break;
                case 2:
                    pictureBox2 .BackgroundImage = bm_2;
                    break;
                default :
                    break;

            }
        }

        private const int WM_NCLBUTTONDOWN = 0xA1;
        private const int HTCAPTION = 2;

        [DllImport("user32.dll")]
        private static extern int SendMessage (int hwnd, int wMsg, int wParam, int lParam);

        [DllImport("user32.dll")]
        private static extern int ReleaseCapture();

        private void form_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //为当前的应用程序释放鼠标捕获
            ReleaseCapture();
            //发送消息,让系统误以为你在标题拦上按下鼠标
            SendMessage((int)this.Handle,WM_NCLBUTTONDOWN,HTCAPTION,0);
        }
    }
}

更多精彩内容其他人还在看

MVC 5限制所有HTTP请求必须是POST方式

这篇文章主要为大家详细介绍了MVC 5限制所有HTTP请求必须是POST方式的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ASP.NET MVC中jQuery与angularjs混合应用传参并绑定数据

这篇文章主要介绍了ASP.NET MVC中jQuery与angularjs混合应用传参并绑定数据,需要的朋友可以参考下
收藏 0 赞 0 分享

Asp.net MVC 对所有用户输入的字符串字段做Trim处理的方法

这篇文章主要介绍了Asp.net MVC 如何对所有用户输入的字符串字段做Trim处理,需要的朋友可以参考下
收藏 0 赞 0 分享

NetCore WebSocket即时通讯示例

这篇文章主要为大家详细介绍了NetCore WebSocket即时通讯示例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

深入理解Asp.Net中WebForm的生命周期

这篇文章主要给大家介绍了关于Asp.Net中WebForm生命周期的相关资料,文中介绍的非常星系,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来学习学习吧。
收藏 0 赞 0 分享

VS2013安装时如何避开IE10的限制

这篇文章主要介绍了VS2013安装时如何避开IE10的限制,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Jexus部署.Net Core项目

这篇文章主要为大家详细介绍了Jexus部署.Net Core项目的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

NetCore1.1+Linux部署初体验

这篇文章主要为大家详细介绍了NetCore1.1+Linux部署的初体验,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Asp.NetCore1.1版本去掉project.json后如何打包生成跨平台包

这篇文章主要为大家详细介绍了Asp.NetCore1.1版本去掉project.json后如何打包生成跨平台包 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

开源跨平台运行服务插件TaskCore.MainForm

这篇文章主要为大家详细介绍了开源跨平台运行服务插件TaskCore.MainForm的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多