C # obtain software icons

Source: Internet
Author: User
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
namespace ShowIcon
{
[StructLayout( LayoutKind.Sequential)]
public struct FileInfomation
{
public IntPtr hIcon;
public int iIcon;
public int dwAttributes;

[ MarshalAs( UnmanagedType.ByValTStr, SizeConst = 260 )]
public string szDisplayName;

[ MarshalAs( UnmanagedType.ByValTStr, SizeConst = 80 )]
public string szTypeName;
}

/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm
{
[STAThread]
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}


[DllImport("shell32.dll", EntryPoint="SHGetFileInfo")]
public static extern int GetFileInfo(string pszPath, int dwFileAttributes, ref FileInfomation psfi, int cbFileInfo,int uFlags);
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
public static Icon GetSmallIcon(string path)
{
FileInfomation _info = new FileInfomation();

GetFileInfo(path, 0, ref _info, Marshal.SizeOf(_info), (int)(0x000000100| 0x000000001));
try
{
return Icon.FromHandle(_info.hIcon);
}
catch
{
return null;
}
}
void Button1Click(object sender, System.EventArgs e)
{
this.openFileDialog1.ShowDialog();
}

void OpenFileDialog1FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
this.textBox1.Text=this.openFileDialog1.FileName;
this.pictureBox1.Image=GetSmallIcon(this.openFileDialog1.FileName).ToBitmap();
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.