从Bing搜索得到,保存于此

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Diagnostics; namespace WpfApplication1
{
public class c_icon_of_path
{
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
private struct POINT
{
public int x;
public int y;
}
// Constants that we need in the function call private const int SHGFI_ICON = 0x100; private const int SHGFI_SMALLICON = 0x1; private const int SHGFI_LARGEICON = 0x0; private const int SHIL_JUMBO = 0x4;
private const int SHIL_EXTRALARGE = 0x2; // This structure will contain information about the file public struct SHFILEINFO
{ // Handle to the icon representing the file public IntPtr hIcon; // Index of the icon within the image list public int iIcon; // Various attributes of the file public uint dwAttributes; // Path to the file [MarshalAs(UnmanagedType.ByValTStr, SizeConst = )] public string szDisplayName; // File type [MarshalAs(UnmanagedType.ByValTStr, SizeConst = )] public string szTypeName; }; [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
public static extern Boolean CloseHandle(IntPtr handle); private struct IMAGELISTDRAWPARAMS
{
public int cbSize;
public IntPtr himl;
public int i;
public IntPtr hdcDst;
public int x;
public int y;
public int cx;
public int cy;
public int xBitmap; // x offest from the upperleft of bitmap
public int yBitmap; // y offset from the upperleft of bitmap
public int rgbBk;
public int rgbFg;
public int fStyle;
public int dwRop;
public int fState;
public int Frame;
public int crEffect;
} [StructLayout(LayoutKind.Sequential)]
private struct IMAGEINFO
{
public IntPtr hbmImage;
public IntPtr hbmMask;
public int Unused1;
public int Unused2;
public RECT rcImage;
} #region Private ImageList COM Interop (XP)
[ComImportAttribute()]
[GuidAttribute("46EB5926-582E-4017-9FDF-E8998DAA0950")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
//helpstring("Image List"),
interface IImageList
{
[PreserveSig]
int Add(
IntPtr hbmImage,
IntPtr hbmMask,
ref int pi); [PreserveSig]
int ReplaceIcon(
int i,
IntPtr hicon,
ref int pi); [PreserveSig]
int SetOverlayImage(
int iImage,
int iOverlay); [PreserveSig]
int Replace(
int i,
IntPtr hbmImage,
IntPtr hbmMask); [PreserveSig]
int AddMasked(
IntPtr hbmImage,
int crMask,
ref int pi); [PreserveSig]
int Draw(
ref IMAGELISTDRAWPARAMS pimldp); [PreserveSig]
int Remove(
int i); [PreserveSig]
int GetIcon(
int i,
int flags,
ref IntPtr picon); [PreserveSig]
int GetImageInfo(
int i,
ref IMAGEINFO pImageInfo); [PreserveSig]
int Copy(
int iDst,
IImageList punkSrc,
int iSrc,
int uFlags); [PreserveSig]
int Merge(
int i1,
IImageList punk2,
int i2,
int dx,
int dy,
ref Guid riid,
ref IntPtr ppv); [PreserveSig]
int Clone(
ref Guid riid,
ref IntPtr ppv); [PreserveSig]
int GetImageRect(
int i,
ref RECT prc); [PreserveSig]
int GetIconSize(
ref int cx,
ref int cy); [PreserveSig]
int SetIconSize(
int cx,
int cy); [PreserveSig]
int GetImageCount(
ref int pi); [PreserveSig]
int SetImageCount(
int uNewCount); [PreserveSig]
int SetBkColor(
int clrBk,
ref int pclr); [PreserveSig]
int GetBkColor(
ref int pclr); [PreserveSig]
int BeginDrag(
int iTrack,
int dxHotspot,
int dyHotspot); [PreserveSig]
int EndDrag(); [PreserveSig]
int DragEnter(
IntPtr hwndLock,
int x,
int y); [PreserveSig]
int DragLeave(
IntPtr hwndLock); [PreserveSig]
int DragMove(
int x,
int y); [PreserveSig]
int SetDragCursorImage(
ref IImageList punk,
int iDrag,
int dxHotspot,
int dyHotspot); [PreserveSig]
int DragShowNolock(
int fShow); [PreserveSig]
int GetDragImage(
ref POINT ppt,
ref POINT pptHotspot,
ref Guid riid,
ref IntPtr ppv); [PreserveSig]
int GetItemFlags(
int i,
ref int dwFlags); [PreserveSig]
int GetOverlayImage(
int iOverlay,
ref int piIndex);
};
#endregion ///
/// SHGetImageList is not exported correctly in XP. See KB316931
/// http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q316931
/// Apparently (and hopefully) ordinal 727 isn't going to change.
///
[DllImport("shell32.dll", EntryPoint = "#727")]
private extern static int SHGetImageList(
int iImageList,
ref Guid riid,
out IImageList ppv
); // The signature of SHGetFileInfo (located in Shell32.dll)
[DllImport("Shell32.dll")]
public static extern int SHGetFileInfo(string pszPath, int dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, uint uFlags); [DllImport("Shell32.dll")]
public static extern int SHGetFileInfo(IntPtr pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, uint uFlags); [DllImport("shell32.dll", SetLastError = true)]
static extern int SHGetSpecialFolderLocation(IntPtr hwndOwner, Int32 nFolder,
ref IntPtr ppidl); [DllImport("user32")]
public static extern int DestroyIcon(IntPtr hIcon); public struct pair
{
public System.Drawing.Icon icon { get; set; }
public IntPtr iconHandleToDestroy { set; get; } } public static int DestroyIcon2(IntPtr hIcon)
{
return DestroyIcon(hIcon);
} private static BitmapSource bitmap_source_of_icon(System.Drawing.Icon ic)
{
var ic2 = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(ic.Handle,
System.Windows.Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
ic2.Freeze();
return ((BitmapSource)ic2);
} //public static BitmapSource SystemIcon(bool small, ShellLib.ShellApi.CSIDL csidl)
//{ // IntPtr pidlTrash = IntPtr.Zero;
// int hr = SHGetSpecialFolderLocation(IntPtr.Zero, (int)csidl, ref pidlTrash);
// Debug.Assert(hr == 0); // SHFILEINFO shinfo = new SHFILEINFO(); // uint SHGFI_USEFILEATTRIBUTES = 0x000000010; // // Get a handle to the large icon
// uint flags;
// uint SHGFI_PIDL = 0x000000008;
// if (!small)
// {
// flags = SHGFI_PIDL | SHGFI_ICON | SHGFI_LARGEICON | SHGFI_USEFILEATTRIBUTES;
// }
// else
// {
// flags = SHGFI_PIDL | SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES;
// } // var res = SHGetFileInfo(pidlTrash, 0, ref shinfo, Marshal.SizeOf(shinfo), flags);
// Debug.Assert(res != 0); // var myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
// Marshal.FreeCoTaskMem(pidlTrash);
// var bs = bitmap_source_of_icon(myIcon);
// myIcon.Dispose();
// bs.Freeze(); // importantissimo se no fa memory leak
// DestroyIcon(shinfo.hIcon);
// CloseHandle(shinfo.hIcon);
// return bs; //} public static BitmapSource icon_of_path(string FileName, bool small, bool checkDisk, bool addOverlay)
{
SHFILEINFO shinfo = new SHFILEINFO(); uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
uint SHGFI_LINKOVERLAY = 0x000008000; uint flags;
if (small)
{
flags = SHGFI_ICON | SHGFI_SMALLICON;
}
else
{
flags = SHGFI_ICON | SHGFI_LARGEICON;
}
if (!checkDisk)
flags |= SHGFI_USEFILEATTRIBUTES;
if (addOverlay)
flags |= SHGFI_LINKOVERLAY; var res = SHGetFileInfo(FileName, , ref shinfo, Marshal.SizeOf(shinfo), flags);
if (res == )
{
throw (new System.IO.FileNotFoundException());
} var myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon); var bs = bitmap_source_of_icon(myIcon);
myIcon.Dispose();
bs.Freeze(); // importantissimo se no fa memory leak
DestroyIcon(shinfo.hIcon);
CloseHandle(shinfo.hIcon);
return bs; } public static BitmapSource icon_of_path_large(string FileName, bool jumbo, bool checkDisk)
{ SHFILEINFO shinfo = new SHFILEINFO(); uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
uint SHGFI_SYSICONINDEX = 0x4000; int FILE_ATTRIBUTE_NORMAL = 0x80; uint flags;
flags = SHGFI_SYSICONINDEX; if (!checkDisk) // This does not seem to work. If I try it, a folder icon is always returned.
flags |= SHGFI_USEFILEATTRIBUTES; var res = SHGetFileInfo(FileName, FILE_ATTRIBUTE_NORMAL, ref shinfo, Marshal.SizeOf(shinfo), flags);
if (res == )
{
throw (new System.IO.FileNotFoundException());
}
var iconIndex = shinfo.iIcon; // Get the System IImageList object from the Shell:
Guid iidImageList = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950"); IImageList iml;
int size = jumbo ? SHIL_JUMBO : SHIL_EXTRALARGE;
var hres = SHGetImageList(size, ref iidImageList, out iml); // writes iml
//if (hres == 0)
//{
// throw (new System.Exception("Error SHGetImageList"));
//} IntPtr hIcon = IntPtr.Zero;
int ILD_TRANSPARENT = ;
hres = iml.GetIcon(iconIndex, ILD_TRANSPARENT, ref hIcon);
//if (hres == 0)
//{
// throw (new System.Exception("Error iml.GetIcon"));
//} var myIcon = System.Drawing.Icon.FromHandle(hIcon);
var bs = bitmap_source_of_icon(myIcon);
myIcon.Dispose();
bs.Freeze(); // very important to avoid memory leak
DestroyIcon(hIcon);
//CloseHandle(hIcon); return bs; }
}
}

C#获取文件超大图标256*256(转)的更多相关文章

  1. QFileInfo与QFileIconProvider(分别用于获取文件信息和获取文件的图标)

    判断文件是否存在,获取文件名称,绝对路径,修改时间等等信息 fileInfo = Qt.QFileInfo(filename) fileIcon = Qt.QFileIconProvider() ic ...

  2. C++根据扩展名获取文件图标、类型

    简述 在Windows系统中,根据扩展名来区分文件类型,比如:.txt(文本文件)..exe(可执行程序).*.zip(压缩文件),下面,我们来根据扩展名来获取对应的文件图标.类型. 简述 源码 源码 ...

  3. 在Vista或更高版本Windows系统中, 获取超大图标的办法

    这几天写个小东西, 需要获取系统正在运行的程序图标, 一般来说32*32就足够了, 不过既然Win7能够支持超大图标(256*256), 咱们也需要与时俱进, 说不定什么时候遇到个变态客户就有这要求了 ...

  4. C# 获取文件图标

    今天突然想到一个问题,如何去获取一个文件的关联图标呢?于是就上网搜索了一下.现总结如下: 首先明确问题:获取一个文件的关联图标或者是某个类型文件的显示图标. 在网上搜了一圈,发现方法还是比较多的,但是 ...

  5. Qt之根据扩展名获取文件图标、类型

    简述 在C++根据扩展名获取文件图标.类型一节中我们分享了如何根据扩展名来获取对应的文件图标.类型,下面.我们在Qt中使用它. 简述 示例 效果 源码 更多参考 示例 如下,我们根据扩展名来获取对应的 ...

  6. Qt之QFileIconProvider(根据扩展名获取文件图标、类型)

    简述 在Qt之QFileIconProvider一节中已经讲解关于如何获取文件图标与类型.但只仍针对本地已存在的文件,此节,我们主要运用前面分享的内容,讲述如何通过任意后缀或本地不存在的文件来获取相关 ...

  7. delphi 动态获取文件类型的图标

    delphi 动态获取文件类型的图标.txt我不奢望什么,只希望你以后的女人一个不如一个.真怀念小时候啊,天热的时候我也可以像男人一样光膀子!在应用程序的编写中,组合框(ComboBox).列表框(L ...

  8. c++ 获取文件图标,类型名称,属性 SHGetFileInfo

    SHGetFileInfo是一个相当实用的Windows API函数. // [MoreWindows工作笔记4] 获取文件图标,类型名称,属性 SHGetFileInfo #include < ...

  9. delphi 获取文件图标

    {根据文件的名字得到此文件在系统中对应大小的图标large=true(64*64) false(32*32)}procedure GetFileIcon(TypeName: Widestring; I ...

随机推荐

  1. Window下mysql环境配置问题整理

    Window下mysql环境配置问题整理 参考如下链接. 无需安装解压版mysql包 创建选项配置 首次启动服务 用mysqld初始化目录 安装后设置和测试 启动服务错误信息 管理员模式打开cmd m ...

  2. SQL SERVER数据库升级手册

    背景 最近接手很多项目都跟数据库升级有关.感触还是颇深,写个心得,供大家参考,如果有疑问欢迎留言.     为什么升级?   你可能会因为各种各样的原因选择升级.我认为原因主要是3个方面 1.旧版本使 ...

  3. python 游戏(猜单词Hangman)

    1.游戏思路和流程图 实现功能:随机一个单词让玩家猜测(后续难度实现修改为成语填空,成语必须要有提示,可修改猜的次数,增加连续猜成语,难度系数随着次数的增加而增加) 游戏流程图 2. 单词库和模块 i ...

  4. Alpha版本项目展示要求(加入模板)

    Alpha版本展示的时间暂定为11月17日课上,提前到13:00开始.如有变动,另行通知. Alpha版本项目展示要求如下: 不得使用PPT,展示所用的资料必须发表在博客上. 现场演示你们发布的软件. ...

  5. 2017-2018 第一学期201623班《程序设计与数据结构》-第9&10周作业问题总结

    一.作业内容 第8周作业 http://www.cnblogs.com/rocedu/p/7484252.html#WEEK08 第9周作业 http://www.cnblogs.com/rocedu ...

  6. RabbitMQ None of the specified endpoints were reachable

    消息队列部署到服务器的时候,需要新增一个用户,然后一定要设置权限.参考一下 https://www.cnblogs.com/gossip/p/4573056.html

  7. 开源中文分词框架分词效果对比smartcn与IKanalyzer

    一.引言: 中文分词一直是自然语言处理的一个痛处,早在08年的时候,就曾经有项目涉及到相关的应用(Lunce构建全文搜索引擎),那时的痛,没想到5年后的今天依然存在,切分效果.扩展支持.业务应用等方面 ...

  8. nodemoduleOfCheerio

    https://www.npmjs.com/package/cheerio 这是npm的api说明 英文好的同学可以去看看. cheerioAPI 是一个用来操作jsDOM的模块: 就像jQuery一 ...

  9. [转帖] 常见的cmd命令

    记录一下 后期用的到. ------------ 1. Echo :显示当前ECHO的状态:ECHO ON 或者ECHO OFF .2. ECHO ON :ECHO状态设为ON,将显示命令行(如每行前 ...

  10. Helm 安装 wordpress

    1. 前置需要安装 storageclass 然后 安装helm 客户端 helm tiller 服务端 2. 设置 当前的位阿里云的 repo 3. 查找 wordpress的镜像 helm sea ...