Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
well lets say i have a link to a file on my desktop say its

C:\Users\pc\AppData\Roaming\Dropbox\bin\Dropbox.exe /home

and I want to get just the icon from that file, like the ico and put it into a picturebox, i tried google for this but, im not sure if im searching it right

if anyone has any links or anything, that would help
Posted
Updated 28-Jul-12 4:05am
v2
Comments
[no name] 28-Jul-12 8:46am    
http://www.codeproject.com/Articles/26824/Extract-icons-from-EXE-or-DLL-files
[no name] 28-Jul-12 9:12am    
It doesn't say what reference im looking for
CodeMaster38 28-Jul-12 9:26am    
I'm not exactly sure what your asking for. Are you asking how to extract an icon from an exe file? Or are you asking for extracting an image from a URL?

I'm confused can you please be more specific and add more info so we can help you?
[no name] 28-Jul-12 9:38am    
this is my code so far


try
{
//Gets the icon associated with the currently executing assembly
//(or pass a different file path and name for a different executable)
Icon appIcon = Icon.ExtractAssociatedIcon(textbox1.Text);
pictureBox1.Image = appIcon.ToBitmap();
}
catch (ArgumentException ae)
{
//handle
}

the textbox holds the file location, good news! a picure comes up, but the bad news is that i get a defult icon, looks blank and dose it for all locations
StianSandberg 28-Jul-12 10:02am    
I tested this and got a nice icon from the executable (prg.exe -> icon.ico)
Try to save the bitmap (as I show in my updated answer) to see if the icon is correct.

C#
using System;
using System.Drawing;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Icon appIcon = Icon.ExtractAssociatedIcon("D:\\prg.exe");
             // Check if icon is null or not
            if (appIcon != null)
            {
               // Save the icon or do something else with it..
               appIcon.ToBitmap().Save("D:\\icon.ico");
            }

            Console.ReadLine();
        }
    }
}
 
Share this answer
 
v2
C#
appIcon.ToBitmap().Save("D:\\icon.ico");
will save a bitmap file renamed to ico, not a valid icon file.

To save it as a valid icon file you will have to use filestream:
C#
// Create a stream
FileStream fs = new FileStream("D:\\icon.ico",
            FileMode.OpenOrCreate);
// Save the icon
appIcon.Save(fs);
// Close the filestream
fs.Close();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900