Update: This approach is only to show one of the hidden gems in the .net framework and not to steal copyrighted icons.
Icons of other programs should only be used after owner approval.
Have you ever seen a file with a pretty icon you wanted to use but you couldn't get a similar one in your program?
If yes, try this method >> Icon.ExtractAssociatedIcon here is the code I wrote that works fine, you will also find the sample attached
[code:c#]
Icon ico;
public Form1()
{
InitializeComponent();
ico = this.Icon;
}
private void btnGetIcon_Click(object sender, EventArgs e)
{
if(dlgOpen.ShowDialog() == DialogResult.OK)
{
ico = Icon.ExtractAssociatedIcon(dlgOpen.FileName);
this.Icon = ico;
pnlIcon.Invalidate();
}
}
private void pnlIcon_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawIcon(ico, pnlIcon.ClientRectangle);
}
[/code]
Leave a Reply