文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>从文件中提取Ico图标,支持拖拽

从文件中提取Ico图标,支持拖拽

时间:2010-08-18  来源:无意创新

调用核心类class ExtractIcon
{
[DllImport(
"Shell32.dll")]
private static extern int SHGetFileInfo
(
string pszPath,
uint dwFileAttributes,
out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags
);

[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon
= IntPtr.Zero; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.LPStr, SizeConst
= 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.LPStr, SizeConst
= 80)]
public string szTypeName;
};

private ExtractIcon()
{
}

private enum SHGFI
{
SmallIcon
= 0x00000001,
LargeIcon
= 0x00000000,
Icon
= 0x00000100,
DisplayName
= 0x00000200,
Typename
= 0x00000400,
SysIconIndex
= 0x00004000,
UseFileAttributes
= 0x00000010
}


public static Icon GetIcon(string strPath, bool bSmall)
{
SHFILEINFO info
= new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if (bSmall)
flags
= SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes;
else
flags
= SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes;

SHGetFileInfo(strPath,
256, out info, (uint)cbFileInfo, flags);
return Icon.FromHandle(info.hIcon);
}
}

 

窗体类
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_DragDrop(object sender, DragEventArgs e)
{
string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();

SaveIcon(path);
}

private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect
= DragDropEffects.Copy;
}
else
{
e.Effect
= DragDropEffects.None;
}
}

private void button1_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (this.openFileDialog1.OpenFile() != null)
{
SaveIcon(
this.openFileDialog1.FileName);

}
}
}

public void SaveIcon(string path)
{
Icon icon
= ExtractIcon.GetIcon(path, false);
string name = path.Substring(path.LastIndexOf("\\"), path.LastIndexOf(".") - path.LastIndexOf("\\") + 1);
string svaepath=Application.StartupPath + name+"ico";
FileStream fs
= null;
try
{
fs
= new FileStream(svaepath, FileMode.Create, FileAccess.Write);
icon.Save(fs);
fs.Flush();
fs.Close();

}
catch (Exception ex)
{

ex.ToString();
MessageBox.Show(
"未知错误","提取失败");
}
finally
{
if (fs != null)
{
fs.Close();
}
}


MessageBox.Show(
"已保存" + svaepath,"提取成功");
}
}

 

下载使用
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载