C# 通过文件扩展名获取图标和描述
作者:C/S框架网  发布日期:2011/05/14 17:06:18
C# 通过文件扩展名获取图标和描述


AttachTool 类源码:

贴图图片

C# Code:

/// <summary>
/// 通过扩展名得到图标和描述
/// </summary>
/// <param name="ext">扩展名</param>
/// <param name="LargeIcon">得到大图标</param>
/// <param name="smallIcon">得到小图标</param>
public static void GetExtsIconAndDescription(string ext, out Icon largeIcon, out Icon smallIcon, out string description)
{
   largeIcon = smallIcon = null;
   description = "";
   var extsubkey = Registry.ClassesRoot.OpenSubKey(ext); //从注册表中读取扩展名相应的子键
   if (extsubkey != null)
   {
      var extdefaultvalue = (string)extsubkey.GetValue(null); //取出扩展名对应的文件类型名称
      
      //未取到值,返回预设图标
      if (extdefaultvalue == null)
      {
         GetDefaultIcon(out largeIcon, out smallIcon);
         return;
      }
      
      var typesubkey = Registry.ClassesRoot.OpenSubKey(extdefaultvalue); //从注册表中读取文件类型名称的相应子键
      if (typesubkey != null)
      {
         description = (string)typesubkey.GetValue(null); //得到类型描述字符串
         var defaulticonsubkey = typesubkey.OpenSubKey("DefaultIcon"); //取默认图标子键
         if (defaulticonsubkey != null)
         {
            //得到图标来源字符串
            var defaulticon = (string)defaulticonsubkey.GetValue(null); //取出默认图标来源字符串
            var iconstringArray = defaulticon.Split(',');
            int nIconIndex = 0;
            if (iconstringArray.Length > 1) int.TryParse(iconstringArray[1], out nIconIndex);
            //得到图标
            System.IntPtr phiconLarge = new IntPtr();
            System.IntPtr phiconSmall = new IntPtr();
            ExtractIconExW(iconstringArray[0].Trim('"'), nIconIndex, ref phiconLarge, ref phiconSmall, 1);
            if (phiconLarge.ToInt32() > 0) largeIcon = Icon.FromHandle(phiconLarge);
            if (phiconSmall.ToInt32() > 0) smallIcon = Icon.FromHandle(phiconSmall);
         }
      }
   }
}





C# Code:

/// <summary>
/// 获取缺省图标
/// </summary>
/// <param name="largeIcon"></param>
/// <param name="smallIcon"></param>
public static void GetDefaultIcon(out Icon largeIcon, out Icon smallIcon)
{
   largeIcon = smallIcon = null;
   System.IntPtr phiconLarge = new IntPtr();
   System.IntPtr phiconSmall = new IntPtr();
   ExtractIconExW(Path.Combine(Environment.SystemDirectory, "shell32.dll"), 0, ref phiconLarge, ref phiconSmall, 1);
   if (phiconLarge.ToInt32() > 0) largeIcon = Icon.FromHandle(phiconLarge);
   if (phiconSmall.ToInt32() > 0) smallIcon = Icon.FromHandle(phiconSmall);
}

//来源:C/S框架网(www.csframework.com) QQ:1980854898




C# Code:

/// Return Type: UINT->unsigned int
///lpszFile: LPCWSTR->WCHAR*
///nIconIndex: int
///phiconLarge: HICON*
///phiconSmall: HICON*
///nIcons: UINT->unsigned int
[DllImportAttribute("shell32.dll", EntryPoint = "ExtractIconExW", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
public static extern uint ExtractIconExW([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(UnmanagedType.LPWStr)] string lpszFile, int nIconIndex, ref IntPtr phiconLarge, ref IntPtr phiconSmall, uint nIcons);

public static void CreateFileIcon(string fileType, out Icon large, out Icon small)
{
   string des;
   
   if (fileType.Trim() == "") //预设图标
   {
      AttachTool.GetDefaultIcon(out large, out small);
   }
   else if (fileType.ToUpper() == ".EXE") //应用程序图标单独获取
   {
      IntPtr l = IntPtr.Zero;
      IntPtr s = IntPtr.Zero;
      
      AttachTool.ExtractIconExW(Path.Combine(Environment.SystemDirectory, "shell32.dll"), 2, ref l, ref s, 1);
      
      large = Icon.FromHandle(l);
      small = Icon.FromHandle(s);
   }
   else //其它类型的图标
   {
      AttachTool.GetExtsIconAndDescription(fileType, out large, out small, out des);
   }
   
   if ((large == null) || (small == null)) //无法获取图标,预设图标
   AttachTool.GetDefaultIcon(out large, out small);
}


//来源:C/S框架网(www.csframework.com) QQ:1980854898




C# Code:

public static byte[] ImageToByte(Image image)
{
   MemoryStream ms = new MemoryStream();
   image.Save(ms, ImageFormat.Bmp);
   byte[] bs = ms.ToArray();
   ms.Close();
   return bs;
}

public static Image ByteToImage(byte[] bs)
{
   MemoryStream ms = new MemoryStream(bs);
   Bitmap bmp = new Bitmap(ms);
   ms.Close();
   return bmp;
}


//来源:C/S框架网(www.csframework.com) QQ:1980854898




C# Code:


[StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct HICON__
{
   public int unused;
}

上一篇 下一篇