C# 获取exe、dll中的图标,支持获取256x256分辨率
在网上找过许多文章,都没有成功获取过大图标,只能获取最大32x32。最后自己尝试了相关的windows api,终于找到一个可用的。
主要用到的C++的PrivateExtractIcons函数,具体说明请看:PrivateExtractIcons function
该函数原文有个说明可能需要注意一下:[This function is not intended for general use. It may be altered or unavailable in subsequent versions of Windows.]
UINT WINAPI PrivateExtractIcons(
_In_ LPCTSTR lpszFile,
_In_ int nIconIndex,
_In_ int cxIcon,
_In_ int cyIcon,
_Out_opt_ HICON *phicon,
_Out_opt_ UINT *piconid,
_In_ UINT nIcons,
_In_ UINT flags
);
C#使用DLL import进行引用。
[DllImport("User32.dll")]
public static extern int PrivateExtractIcons(
string lpszFile, //文件名可以是exe,dll,ico,cur,ani,bmp
int nIconIndex, //从第几个图标开始获取
int cxIcon, //获取图标的尺寸x
int cyIcon, //获取图标的尺寸y
IntPtr[] phicon, //获取到的图标指针数组
int[] piconid, //图标对应的资源编号
int nIcons, //指定获取的图标数量,仅当文件类型为.exe 和 .dll时候可用
int flags //标志,默认0就可以,具体可以看LoadImage函数
);
说明:如果想要获取的图标尺寸为256x256,而实际资源文件中的图标尺寸小于256x256的话,则会获取存在的最高分标率,色彩数最丰富的的图标,并拉伸。
具体代码如下
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks; namespace TryGetExeLargeIcon
{
class Program
{
[STAThread]
static void Main(string[] args)
{
//选择文件对话框
var opfd = new System.Windows.Forms.OpenFileDialog { Filter = "资源文件|*.exe;*.dll" };
if (opfd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
var file = opfd.FileName; //指定存放图标的文件夹
const string folderToSave = "D:\\temp\\";
if (!Directory.Exists(folderToSave)) Directory.CreateDirectory(folderToSave); //选中文件中的图标总数
var iconTotalCount = PrivateExtractIcons(file, , , , null, null, , ); //用于接收获取到的图标指针
IntPtr[] hIcons = new IntPtr[iconTotalCount];
//对应的图标id
int[] ids = new int[iconTotalCount];
//成功获取到的图标个数
var successCount = PrivateExtractIcons(file, , , , hIcons, ids, iconTotalCount, ); //遍历并保存图标
for (var i = ; i < successCount; i++)
{
//指针为空,跳过
if (hIcons[i] == IntPtr.Zero) continue; using (var ico = Icon.FromHandle(hIcons[i]))
{
using (var myIcon = ico.ToBitmap())
{
myIcon.Save(folderToSave + ids[i].ToString("") + ".png", ImageFormat.Png);
}
}
//内存回收
DestroyIcon(hIcons[i]);
}
} //details: https://msdn.microsoft.com/en-us/library/windows/desktop/ms648075(v=vs.85).aspx
//Creates an array of handles to icons that are extracted from a specified file.
//This function extracts from executable (.exe), DLL (.dll), icon (.ico), cursor (.cur), animated cursor (.ani), and bitmap (.bmp) files.
//Extractions from Windows 3.x 16-bit executables (.exe or .dll) are also supported.
[DllImport("User32.dll")]
public static extern int PrivateExtractIcons(
string lpszFile, //file name
int nIconIndex, //The zero-based index of the first icon to extract.
int cxIcon, //The horizontal icon size wanted.
int cyIcon, //The vertical icon size wanted.
IntPtr[] phicon, //(out) A pointer to the returned array of icon handles.
int[] piconid, //(out) A pointer to a returned resource identifier.
int nIcons, //The number of icons to extract from the file. Only valid when *.exe and *.dll
int flags //Specifies flags that control this function.
); //details:https://msdn.microsoft.com/en-us/library/windows/desktop/ms648063(v=vs.85).aspx
//Destroys an icon and frees any memory the icon occupied.
[DllImport("User32.dll")]
public static extern bool DestroyIcon(
IntPtr hIcon //A handle to the icon to be destroyed. The icon must not be in use.
);
}
}
C# 获取exe、dll中的图标,支持获取256x256分辨率的更多相关文章
- C#获取C# DLL中的指定接口的所有实现实例 - qq_19759475的博客 - CSDN博客
原文:C#获取C# DLL中的指定接口的所有实现实例 - qq_19759475的博客 - CSDN博客 public static List<T> CreateTarInterface& ...
- javascript获取iframe框架中页面document对象,获取子页面里面的内容,iframe获取父页面的元素,
javascript获取iframe框架中,加载的页面document对象 因为浏览器安全限制,对跨域访问的页面,其document对象无法读取.设置属性 function getDocument(i ...
- bat 获取 exe 文件中 产品版本号并存储到变量中
set EXE='D:\gitlab\drivereasy3\DriverEasyWPF\bin\Release\DriverEasy.exe' powershell "(Get-Item ...
- 获取当前div中的文本(只获取当前div的, 子元素不要, 基于layui)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- .Net中把图片等文件放入DLL中,并在程序中引用
原文:.Net中把图片等文件放入DLL中,并在程序中引用 [摘要] 有时我们需要隐藏程序中的一些资源,比如游戏,过关后才能看到图片,那么图片就必须隐藏起来,否则不用玩这个游戏就可以看到你的图片了,呵呵 ...
- vim 正则表达式获取双引号中的字符
vim 正则表达式获取双引号中的字符 1.获取双引号中的字符 :%s/.*\".∗\".*/\1/ 2.用字符串建立标签 如 hello <hello></ ...
- AutoCAD.NET 不使用P/Invoke方式调用acad.exe或accore.dll中的接口(如acedCommand、acedPostCommand等)
使用C#进行AutoCAD二次开发,有时候由于C#接口不够完善,或者低版本AutoCAD中的接口缺少,有些工作不能直接通过C#接口来实现,所以需要通过P/Invoke的方式调用AutoCAD的其他DL ...
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
Visual Studio 2017中使用正则修改部分内容 最近在项目中想实现一个小工具,需要根据类的属性<summary>的内容加上相应的[Description]特性,需要实现的效 ...
- PyQt(Python+Qt)学习随笔:model/view架构中支持QListView列表中展示图标的两种方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在QListView列表视图中的项不但可以展示文字,也可以展示图标和复选框,同时可以指定项是否可以拖 ...
随机推荐
- input file样式修改,图片预览删除功能
本篇对input file进行了修改,改成自己需要的样式,类似验证身份上传身份证图片的功能. 效果图如下: 这里主要展示上传预览图片功能,对于删除功能的html及css写的比较粗糙,对于想要精细表现这 ...
- HPU--1280 Divisible
题目描述 给定一个很大的整数,我想知道它能否被9整除. 输入 有t组测试数据,每组数据给定一个整数N不存在前导0.(1 <= t <= 20,1 <= N <= 10^200) ...
- java异常捕获的一点感悟
class Annoyance extends Exception {} class Sneeze extends Annoyance {} class Human { public static v ...
- C#设计模式(2)-简单工厂模式
引言 上一遍中介绍了设计模式中的单例模式-C#设计模式(1)-单例模式,本篇将介绍简单工厂模式,也是比较容易理解的一种模式: 简单工厂模式简介 什么是简单工厂模式? 定义一个工厂类,它可以根据参数的不 ...
- python实战===生成随机数
用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限. import random print random.uniform(10, 20) print random.u ...
- Xen创建新虚拟机
一.添加一个ISO存储: 右键选择"New Storage Repository-" 选择"ISO Library"中的"Windows File S ...
- 甲方VS乙方
甲方与乙方,在很多人都存在有误解,不知道究竟如何辨别.这里我提一些简单的辨别方式:甲方一般是指提出目标的一方,在合同拟订过程中主要是提出要实现什么目标,乙方一般是指完成目标,在合同中主要是提出如何保证 ...
- linux下访问windows的共享
linux下,要访问windows的共享,有好几种情况,下面进行总结. 外面的文章,有点介绍需要使用samba,有点说什么都不需要,只需要mount加上参数, 功能是实现,不过原理,还是不太明白,我发 ...
- 优秀开源软件学习系列(一)——从零学习Spring4以及学习方法分享
一.目的1.掌握Spring4怎样使用,以便将这个框架作为自己的一项技能.2.掌握Spring官网是怎样介绍其产品的,在心中对Spring有最官方的.最直观的了解.在Spring的相关领域,能够知道怎 ...
- tomcat websocket 实现网页在线即时聊天
背景介绍 近一个月完成了公司的一个项目,负责即时聊天部分 寻找了一下,决定使用websocket,要问原因的话,因为tomcat 自带相关消息收发的API,用起来方便 闲话少叙,进入实现步骤 使用工具 ...