zoukankan      html  css  js  c++  java
  • [工具-002]把png图片转换成ico图标

      最近我收到了上级的一个需求,我们需要使用产品的png图片,批量转换成ico图片,然后调用上一篇的方法,替换可执行程序的图标。一开始查看资料的时候,C#有直接可以转成ico图片的方法,很简单。但是生成的质量不敢恭维。后面蔡领导不满意,重新寻找解决办法,找了一些C++代码,实现相当麻烦,也很不方便。后面找到了一个png2ico.exe的东西,然后就想到用C#调用CMD命令来完成。好吧,下面讲讲实现步骤。

      

            /**
             * 进行转换
             * */
            private void btnOk_Click(object sender, EventArgs e)
            {
                // 选择生成的图标的大小
                Boolean is128 = this.chk128.Checked;
                Boolean is64  = this.chk64.Checked;
                Boolean is48  = this.chk48.Checked;
                Boolean is32  = this.chk32.Checked;
                Boolean is16  = this.chk16.Checked;
    
                if (String.IsNullOrEmpty(txtInputPath.Text.Trim()))
                {
                    MessageBox.Show("请选择文件!!!");
                    return;
                }
                if (String.IsNullOrEmpty(txtOutputPath.Text.Trim()))
                {
                    MessageBox.Show("请选择存放路径!!!");
                    return;
                }
                // 都没有选择
                if (!is128 && !is64 && !is48 && !is32 && !is16)
                {
                    MessageBox.Show("请选择尺寸!!!");
                    return;
                }
    
                // 进行转换操作
                // 先把要转换的文件拷贝到临时文件夹
                if (!Directory.Exists(TEMPFLODER))
                    Directory.CreateDirectory(TEMPFLODER);
    
                string[] filenames = Directory.GetFiles(TEMPFLODER);
                // 删除其他文件
                foreach (string filename in filenames)
                {
                    File.Delete(filename);
                }
    
                // 把你指定的文件拷贝进去
                string filepath      = txtInputPath.Text.Trim();
                string filenameNoExt = Path.GetFileNameWithoutExtension(filepath);
                string newfilepath = TEMPFLODER + "\" + Path.GetFileName(filepath);
                if (File.Exists(filepath))
                {
                    File.Copy(filepath, newfilepath);
                }
    
                // 开始进行转换
                string outputpath = txtOutputPath.Text.Trim();
                if(is128)
                {
                    translateIco(TEMPFLODER, outputpath, 128);
                    renamefile(outputpath + "\" + filenameNoExt + ".ico", 128);
                }
                if(is64)
                {
                    translateIco(TEMPFLODER, outputpath, 64);
                    renamefile(outputpath + "\" + filenameNoExt + ".ico", 64);
                }
                if(is48)
                {
                    translateIco(TEMPFLODER, outputpath, 48);
                    renamefile(outputpath + "\" + filenameNoExt + ".ico", 48);
                }
                if(is32)
                {
                    translateIco(TEMPFLODER, outputpath, 32);
                    renamefile(outputpath + "\" + filenameNoExt + ".ico", 32);
                }
                if(is16)
                {
                    translateIco(TEMPFLODER, outputpath, 16);
                    renamefile(outputpath + "\" + filenameNoExt + ".ico", 16);
                }
    
                // 删除临时文件
                if (Directory.Exists(TEMPFLODER))
                {
                    string[] files = Directory.GetFiles(TEMPFLODER);
                    // 删除其他文件
                    foreach (string file in files)
                    {
                        File.Delete(file);
                    }
                }
            }
    
            /**
             * 文件转储
             * */
            private void renamefile(String path, int size)
            {
                if (File.Exists(path))
                {
                    // 指定文件名称
                    string filename  = Path.GetFileNameWithoutExtension(path);
                    string filenameN = filename + "" + size + ".ico";
                    // 获取存放路径
                    string floder    = txtOutputPath.Text.Trim();
                    if (File.Exists(floder + "\" + filenameN))
                        File.Delete(floder + "\" + filenameN);
                    // 进行路径复制
                    File.Move(path, floder + "\" + filenameN);
                }
            }
    
            /**
             * 图标转换
             * */
            private void translateIco(String inputpath, String outputpath, int size)
            {
                // 第一个参数输入路径 第二个输出路径  第三个图标大小
                runcommand("png2ico.exe -i "" + inputpath + ""  -o "" + outputpath + ""  -s " + size + " 32bpp -noconfirm");
            }
    
            /**
             * 运行命令
             * */
            private void runcommand(String command)
            {
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                // 不弹出dos窗口
                p.StartInfo.CreateNoWindow = true;
                // dos工作环境
                p.StartInfo.WorkingDirectory = Application.StartupPath;
                try
                {
                    p.Start();
                    Console.WriteLine("command:" + command);
                    p.StandardInput.WriteLine(command + "&exit");
                    string output = p.StandardOutput.ReadToEnd();
                    p.WaitForExit();
                    p.Close();
                    Console.WriteLine("result:" + output);
    
                }
                catch (Exception e1)
                {
                    Console.WriteLine("error" + e1.Message);
                }
           }        
    

       上面的例子中,我们用到了一个方法,就是把要转换的png拷贝到一个临时目录,在把这个临时目录当成是输入路径,然后指定输出路径进行输出。要这么麻烦的一个主要原因是这个png2ico.exe只认文件夹,不认文件,而且会遍历文件夹下的所有png图片进行转换,所以我们创建一个临时目录解决这个问题。得出来的结果跟网上的转换器效果一模一样,蔡主管非常满意,我也很开心。上面的代码提供大家参考,可以批评指正。

     

         结语

    • 受益,C++能操作的底层更多,学会了打DLL包

     

     

    本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 

    转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4512674.html

     

  • 相关阅读:
    OCP-1Z0-051-V9.02-55题
    OCP-1Z0-051-V9.02-60题
    OCP-1Z0-053-V12.02-59题
    OCP-1Z0-053-V12.02-184题
    OCP-1Z0-053-V12.02-595题
    OCP-1Z0-053-V12.02-584题
    OCP-1Z0-053-V12.02-234题
    OCP-1Z0-053-V12.02-548题
    OCP-1Z0-053-V12.02-549题
    OCP-1Z0-053-V12.02-551题
  • 原文地址:https://www.cnblogs.com/superdo/p/4512674.html
Copyright © 2011-2022 走看看