C#获取从指定的文件路径,获取该文件的图标,然后显示在image里 C#获取从指定的文件路径,获取该文件的图标,然后显示在image里例如,获取某个文件的图标,显示在toolstrip的imagebutton里,怎么实现 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 [DllImport("shell32.DLL", EntryPoint = "ExtractAssociatedIcon")] private static extern int ExtractAssociatedIconA(int hInst, string lpIconPath, ref int lpiIcon); //声明函数 System.IntPtr thisHandle; public System.Drawing.Icon SetIcon(string path) { int RefInt = 0; thisHandle = new IntPtr(ExtractAssociatedIconA(0,path, ref RefInt)); return System.Drawing.Icon.FromHandle(thisHandle); } magebutton.Image=Image.FromFile(路径); 楼主是要文件类型的ICO图标吧。顶1楼的。学习 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace WindowsApplication1{ public partial class Form2 : Form { public Form2() { InitializeComponent(); } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] struct SHFILEINFO { public IntPtr hIcon; public int iIcon; public uint dwAttributes; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string szDisplayName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] public string szTypeName; } private void button1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = openFileDialog1.FileName; } using (Graphics g = this.pictureBox1.CreateGraphics()) { g.Clear(this.pictureBox1.BackColor); //清除picturebox的背景色,为了画透明图标 g.DrawIcon(GetIcon(this.textBox1.Text, true), 1, 1); //绘制小图标 } } static Icon GetIcon(string fileName, bool isLargeIcon) { SHFILEINFO shfi = new SHFILEINFO(); IntPtr hI; if (isLargeIcon) hI = NativeMethods.SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_USEFILEATTRIBUTES | NativeMethods.SHGFI_LARGEICON); else hI = NativeMethods.SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_USEFILEATTRIBUTES | NativeMethods.SHGFI_SMALLICON); Icon icon = Icon.FromHandle(shfi.hIcon).Clone() as Icon; NativeMethods.DestroyIcon(shfi.hIcon); //释放资源 return icon; } class NativeMethods { [DllImport("Shell32.dll", EntryPoint = "SHGetFileInfo", SetLastError = true, CharSet = CharSet.Auto)] public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags); [DllImport("User32.dll", EntryPoint = "DestroyIcon")] public static extern int DestroyIcon(IntPtr hIcon); public const uint SHGFI_ICON = 0x100; public const uint SHGFI_LARGEICON = 0x0; //大图标 32×32 public const uint SHGFI_SMALLICON = 0x1; //小图标 16×16 public const uint SHGFI_USEFILEATTRIBUTES = 0x10; } }} 我是想动态的向toolstrip里添加imagebutton,然后设置各项参数;ToolStripButton tsb = new ToolStripButton("aa"); tsb.Image = //这个是设置imagebutton的图片,是从指定的EXE文件里获得图片。不知道怎么弄 tsb.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText; tsb.ImageScaling = ToolStripItemImageScaling.None; tsb.TextImageRelation = TextImageRelation.ImageAboveText; tsb.Text ="名称"; toolStrip1.Items.Add(tsb);还有就是我动态添加的imagebutton,在退出程序,再打开的时候就没有了,还要重新添加,这是怎么回事? 还有怎么向添加的imagebutton写点击事件, 多谢各位的回答,问题解决了Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(path); Image image = icon.ToBitmap();pix.image=image这样就OK了 asp.net 导出word代码问题 c# foreach循环 C# winform中有windows那样风格的时间日期控件么? ★★★为什么绝大多数系统的用户名和密码不支持中文?★★★ asp.net和winform程序调用同样的接口问题 这样会有什么结果(向了解asp.net运行过程的大虾请教) 用C#编写的B/s三层结构分销管理系统源码. 关于下拉框绑定数据集的小问题--急啊 C#中怎样用DirectX,有好用的类封装吗? C#的桌面程序能力 新人求助登陆系统连数据库的问题,求高手完善下 求解一个方法
private static extern int ExtractAssociatedIconA(int hInst, string lpIconPath, ref int lpiIcon); //声明函数
System.IntPtr thisHandle;
public System.Drawing.Icon SetIcon(string path)
{
int RefInt = 0;
thisHandle = new IntPtr(ExtractAssociatedIconA(0,path, ref RefInt));
return System.Drawing.Icon.FromHandle(thisHandle);
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace WindowsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
}
using (Graphics g = this.pictureBox1.CreateGraphics())
{
g.Clear(this.pictureBox1.BackColor); //清除picturebox的背景色,为了画透明图标 g.DrawIcon(GetIcon(this.textBox1.Text, true), 1, 1); //绘制小图标
}
}
static Icon GetIcon(string fileName, bool isLargeIcon)
{
SHFILEINFO shfi = new SHFILEINFO();
IntPtr hI; if (isLargeIcon)
hI = NativeMethods.SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_USEFILEATTRIBUTES | NativeMethods.SHGFI_LARGEICON);
else
hI = NativeMethods.SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_USEFILEATTRIBUTES | NativeMethods.SHGFI_SMALLICON); Icon icon = Icon.FromHandle(shfi.hIcon).Clone() as Icon; NativeMethods.DestroyIcon(shfi.hIcon); //释放资源
return icon;
}
class NativeMethods
{ [DllImport("Shell32.dll", EntryPoint = "SHGetFileInfo", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags); [DllImport("User32.dll", EntryPoint = "DestroyIcon")]
public static extern int DestroyIcon(IntPtr hIcon); public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; //大图标 32×32
public const uint SHGFI_SMALLICON = 0x1; //小图标 16×16
public const uint SHGFI_USEFILEATTRIBUTES = 0x10;
}
}
}
tsb.Image = //这个是设置imagebutton的图片,是从指定的EXE文件里获得图片。不知道怎么弄
tsb.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
tsb.ImageScaling = ToolStripItemImageScaling.None;
tsb.TextImageRelation = TextImageRelation.ImageAboveText;
tsb.Text ="名称";
toolStrip1.Items.Add(tsb);还有就是我动态添加的imagebutton,在退出程序,再打开的时候就没有了,还要重新添加,这是怎么回事? 还有怎么向添加的imagebutton写点击事件,
Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(path);
Image image = icon.ToBitmap();
pix.image=image
这样就OK了