当前位置: 首页 > news >正文

C#将图片转换为ICON格式(程序运行图标)

介绍:

C#创建窗体项目后左上角有显示图标,这个图标会在运行的时候显示在下面进程这里,但是必须是ico格式的图片才可以导入使用。以下是将图片打开后保存为ico格式代码。

代码如下:

main函数测试

new 将图片转换成icon格式(@"C:\\Users\\admin\\Desktop\\公司logo", "PNG");

定义类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
using System.IO;namespace MyFrom
{internal class 将图片转换成icon格式{public 将图片转换成icon格式(string picFileName, string fileType = "jpg", int width = 0, int hight = 0) {SaveToIcon(picFileName, fileType, width, hight);}public bool SaveToIcon(string picFileName, string fileType = "jpg", int width = 0, int hight = 0){Bitmap pic;string file = picFileName + "." + fileType;if (!File.Exists(file)) {MessageBox.Show("当前目录不存在该图片");return false;}try{if (width <= 0 || hight <= 0){using (Image img = Image.FromFile(file)){pic = new Bitmap(img);}}else{using (Image img = Image.FromFile(file)){pic = new Bitmap(img, width, hight);}}}catch(Exception ex){MessageBox.Show("图片打开错误:" + ex.Message);return false;}Icon icon = Icon.FromHandle(pic.GetHicon());System.IO.Stream stream = new System.IO.FileStream(picFileName + ".ico", System.IO.FileMode.Create);icon.Save(stream);stream.Flush();stream.Close();return true;}}
}

http://www.lryc.cn/news/199486.html

相关文章:

  • ELK架构Logstash的相关插件:grok、multiline、mutate、date的详细介绍
  • linux 防火墙介绍以及iptables的使用
  • 原码、反码、补码在汇编中的应用
  • 【红日靶场】vulnstack5-完整渗透过程
  • 嵌入式平台的电源总结
  • @Binds methods must be abstract 报错指南
  • 自定义反序列化类将LocalDate时间格式转为 LocalDateTime
  • MySQL JSON_TABLE() 函数
  • 【MATLAB第80期】基于MATLAB的结构核岭回归SKRR多输入单输出回归预测及分类预测模型
  • Qt消息对话框的使用
  • spring的Ioc、DI以及Bean的理解
  • 倒计时 天时分秒
  • Spring篇---第六篇
  • 【unity小技巧】适用于任何 2d 游戏的钥匙门系统和buff系统——UnityEvent的使用
  • 爬虫ip如何加入到代码里实现自动化数据抓取
  • 在win10上安装配置Hadoop的环境变量
  • MAX插件CG Magic怎么云渲染?操作方法已整起!
  • 尝试使用jmeter-maven-plugin
  • navigator.userAgent.toLowerCase区分设备,浏览器
  • 防火墙操作:开放端口ICMP时间戳请求漏洞修复
  • MySQL配置环境变量和启动登录
  • 救济金发放(The Dole Queue, UVa 133)rust解法
  • oracle实验四
  • 数据结构-堆排序Java实现
  • C#进阶——反射(Reflection)
  • Oracle 运维篇+应用容器数据库的install、upgrade、patch、uninstall
  • Affinity Publisher for Mac/Windows最新中文下载 排版神器
  • Mac文件对比同步工具 Beyond Compare 4.4.7
  • 技巧 | 如何解决 OBS 系统声音无法捕获问题 | Mac
  • 从头开始机器学习:逻辑回归