public void IconExtra(string filepath,string icopath)
{
//IntPtr hInst;
    IntPtr hIcon = ExtractIcon(IntPtr.Zero,filepath,1);
            Icon smallico = Icon.FromHandle(hIcon);
FileStream icofile = new FileStream(icopath,FileMode.Create,FileAccess.Write);
smallico.Save(icofile);
icofile.Close();

提出的图标是32*32的,而我需要16*16的,并且提出的图标是黑白的,不知为什么,请指点一二!

解决方案 »

  1.   

    晕倒,你的代码是经过封装的,把那个ExtractIcon的代码贴出来看看吧
      

  2.   

    ExtractIcon是api函数呀
    我就这么写的
    [DllImport("Shell32.dll", CharSet=CharSet.Auto)]
    public static extern IntPtr ExtractIcon(IntPtr hInstance,string strFileName,uint uiIconIndex);
      

  3.   

    刚刚看了一下MSDN,你可能需要使用ExtractIconEx 这个API,这个是可以指定提取big或者small的,另外你的保存方法可能会有问题。我还来不及研究这个问题。
      

  4.   

    我也看过这个,但我搞不清楚big算多大的 small算多小的,不光是16*16和32*32的
      

  5.   

    public const int SHGFI_ICON              = 0x000000100;     // get icon
    public const int SHGFI_DISPLAYNAME       = 0x000000200;     // get display name
    public const int SHGFI_TYPENAME          = 0x000000400;     // get type name
    public const int SHGFI_ATTRIBUTES        = 0x000000800;     // get attributes
    public const int SHGFI_ICONLOCATION      = 0x000001000;     // get icon location
    public const int SHGFI_EXETYPE           = 0x000002000;     // return exe type
    public const int SHGFI_SYSICONINDEX      = 0x000004000;     // get system icon index
    public const int SHGFI_LINKOVERLAY       = 0x000008000;     // put a link overlay on icon
    public const int SHGFI_SELECTED          = 0x000010000;     // show icon in selected state
    public const int SHGFI_ATTR_SPECIFIED    = 0x000020000;     // get only specified attributes
    public const int SHGFI_LARGEICON         = 0x000000000;     // get large icon
    public const int SHGFI_SMALLICON         = 0x000000001;     // get small icon
    public const int SHGFI_OPENICON          = 0x000000002;     // get open icon
    public const int SHGFI_SHELLICONSIZE     = 0x000000004;     // get shell size icon
    public const int SHGFI_PIDL              = 0x000000008;     // pszPath is a pidl
    public const int SHGFI_USEFILEATTRIBUTES = 0x000000010;     // use passed dwFileAttribute[StructLayout(LayoutKind.Sequential)]
    public struct SHFILEINFO
    {
    public IntPtr hIcon;
    public int iIcon;
    public int dwAttr;
    public string szDisplayName;
    public string szTypeName;
    }[DllImport("shell32.dll",EntryPoint="SHGetFileInfo")]
    public static extern int SHGetFileInfo(string pszPath, int dwFileAttr, ref SHFILEINFO psfi, int cbFileInfo, int uFlags);[DllImport("user32.dll",EntryPoint="DestroyIcon")]
    public static extern bool DestroyIcon(IntPtr hIcon);//调用
    SHFILEINFO fileInfo = new SHFILEINFO();
    SHGetFileInfo(strFileName, 0,ref fileInfo, 
    System.Runtime.InteropServices.Marshal.SizeOf(fileInfo), 
    SHGFI_ICON | SHGFI_LARGEICON);//这里可选择要使用的图标类型Icon icon = Icon.FromHandle(fileInfo.hIcon);
    DestroyIcon(fileInfo.hIcon);