Extracting Icons from Files

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]

IconExtractor.zip (7.03 kb)

This entry was posted in Windows Forms. Bookmark the permalink.

3 Responses to Extracting Icons from Files

  1. Bob Saggett says:

    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? Do you think that other artists are simply to be copied, regardless of the hours of work that they put in to create the icon? Is copyright law just a scam to protect big industrial players and not to protect the little guys too?

    Answer YES to all three? Well, go ahead and rip off my work, thanks.

  2. Amr says:

    @Bob Saggett
    No, Ofcourse the post has been updated due to your comment, thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>