首页 > 编程知识 正文

Png转ico,如何将png转为ico

时间:2023-05-06 16:20:48 阅读:247070 作者:1858

    对程序员而言,不给自己写的程序配个好看的图标,就好比没舍得给小闺女花时间扎个辫子一样,养都养了,就不能养好看些吗?

可惜photoshop需要插件才能到处ico文件,网上有工具但是用起来还是比较麻烦的,干脆自己写一个吧!

转换用的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace IcoMaker
{
public static class HelperIcon
{
private static byte[] pngiconheader =
new byte[] { 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
public static Icon PngIconFromImage(Image img, int size = 16)
{
using (Bitmap bmp = new Bitmap(img, new Size(size, size)))
{
byte[] png;
using (System.IO.MemoryStream fs = new System.IO.MemoryStream())
{
bmp.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
fs.Position = 0;
png = fs.ToArray();
}

using (System.IO.MemoryStream fs = new System.IO.MemoryStream())
{
if (size >= 256) size = 0;
pngiconheader[6] = (byte)size;
pngiconheader[7] = (byte)size;
pngiconheader[14] = (byte)(png.Length & 255);
pngiconheader[15] = (byte)(png.Length / 256);
pngiconheader[18] = (byte)(pngiconheader.Length);

fs.Write(pngiconheader, 0, pngiconheader.Length);
fs.Write(png, 0, png.Length);
fs.Position = 0;
return new Icon(fs);
}
}
}
}
}

调用方法:

OpenFileDialog _openDialog = new OpenFileDialog();
//打开图像类型
_openDialog.Filter = "*.PNG|*.PNG";
_openDialog.Multiselect = false;
_openDialog.ShowDialog();
if (_openDialog.FileName != "")
{
//将打开的图片存入Bitmap中
System.Drawing.Bitmap bmpIco = new System.Drawing.Bitmap(_openDialog.FileName);

SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "图标(*.ico)|*.ico";
dlg.CheckPathExists = true;
dlg.ShowDialog();
if (!string.IsNullOrEmpty(dlg.FileName))
{
System.IO.FileStream fs = new System.IO.FileStream(dlg.FileName, System.IO.FileMode.Create);
System.Drawing.Icon icon = HelperIcon.PngIconFromImage(bmpIco, 64);

//将Icon保存的指定的输出
icon.Save(fs);
fs.Close();
MessageBox.Show("图标生成成功!");
}
}

转载于:https://www.cnblogs.com/iamai/p/11171108.html

k8s使用nfs持久化数据logrotate-logrotate配置

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。