【无标题】

Unity 读取exe文件图标
工程需要导入 System.Drawing.dll, 位置可百度
以下两个方式都可调用。
1、public class LoadExeIcon{
private static extern int ExtractAssociatedIconA(int hInst, string lpIconPath, ref int lpiIcon); //声明函数
static System.IntPtr thisHandle;
public static System.Drawing.Bitmap GetIcon(string path)
{
int RefInt = 0;
thisHandle = new IntPtr(ExtractAssociatedIconA(0, path, ref RefInt));
System.Drawing.Bitmap image = System.Drawing.Icon.FromHandle(thisHandle).ToBitmap();
return image;
}
}

2、
直接添加函数 ,传入参数为文件名称(可全路径)
public static Icon GetIconByFileName(string fileName, bool isLarge = true)
{
int[] phiconLarge = new int[1];
int[] phiconSmall = new int[1];
//文件名 图标索引
DllOpenFileDialog.ExtractIconEx(fileName, 0, phiconLarge, phiconSmall, 1);
IntPtr IconHnd = new IntPtr(isLarge ? phiconLarge[0] : phiconSmall[0]);
return Icon.FromHandle(IconHnd);
}

调用方式:
System.Drawing.Bitmap bmp = DllOpenFileDialog.GetIcon(“E:\hfs.exe”);
//System.Drawing.Bitmap bmp = GetIconByFileName(“E:\hfs.exe”, false).ToBitmap();

// bitmap转Unity sprite
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Png);
var buffer = new byte[ms.Length];
ms.Position = 0;
ms.Read(buffer, 0, buffer.Length);
Texture2D newTex = new Texture2D(1, 1);
newTex.LoadImage(buffer);

Sprite s = Sprite.Create(newTex, new Rect(0, 0, newTex.width, newTex.height), Vector2.zero);

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值