• C# 图片格式(JPG,BMP,PNG,GIF)等转换为ICO图标


    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.IO;

    namespace ICOTest
    {
        public partial class Form1 : Form
        {
            string FileName = "C:\\";
            public Form1()
            {
                InitializeComponent();
            }

            //核心代码
            private void button1_Click(object sender, EventArgs e)
            {
                Size size = new Size(32, 32);
                //获得原始图片文件
                using (Bitmap bm = new Bitmap(FileName))
                {
                    //从现有图像缩小,为了得到合适的ICO文件
                    using (Bitmap iconBm = new Bitmap(bm, size))
                    {
                        //如果是windows调用,直接下面一行代码就可以了
                        //此代码不能在web程序中调用,会有安全异常抛出
                        using (Icon icon = Icon.FromHandle(iconBm.GetHicon()))
                        {
                            string Name_ = "C:\\ICO\\" + DateTime.Now.Ticks.ToString() + ".ico";
                            using (Stream stream = new System.IO.FileStream(Name_, System.IO.FileMode.Create))
                            {

                                icon.Save(stream);
                                MessageBox.Show("转换成功。路径是:" + Name_);
                            }
                        }
                    }
                }
            }

            private void button2_Click(object sender, EventArgs e)
            {
                openFileDialog1.Filter = "JPG|*.jpg|BMP|*.bmp|PNG|*.png|GIF|*.gif";
                DialogResult D = openFileDialog1.ShowDialog();
                if (D == DialogResult.OK)
                {
                    FileName = this.openFileDialog1.FileName.Trim();
                    this.textBox1.Text = FileName;
                }
            }
        }
    }

    图像界面效果如下:

  • 相关阅读:
    海云健康:上云为10万家药店带去了什么价值?
    PostgreSQL数据目录深度揭秘
    当设计模式遇上 Hooks
    SpringBoot 优雅停止服务的几种方法
    【2021-08-19】连岳摘抄
    【2021-08-18】该休息,还是要好好休息
    【2021-08-17】心里没底,就给它得找个底
    NFLSOJ 1072
    Solution -「ARC 125F」Tree Degree Subset Sum
    Solution -「ARC 125E」Snack
  • 原文地址:https://www.cnblogs.com/bingyun84/p/1897240.html
Copyright © 2020-2023  润新知