Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Found info <a href="http://www.geekpedia.com/tutorial219_Extracting-Icons-from-Files.html" rel="nofollow noreferrer">here</a>. To get the large Vista icon, you need to use Shell32's SHGetFileInfo method. I've copied the relevant text below, of course you'll want to replace the filename variable with "Assembly.GetExecutingAssembly().Location".</p> <pre><code>using System.Runtime.InteropServices; </code></pre> <p>A bunch of constants we will use in the call to SHGetFileInfo() to specify the size of the icon we wish to retrieve:</p> <pre><code>// Constants that we need in the function call private const int SHGFI_ICON = 0x100; private const int SHGFI_SMALLICON = 0x1; private const int SHGFI_LARGEICON = 0x0; </code></pre> <p>The SHFILEINFO structure is very important as it will be our handle to various file information, among which is the graphic icon. </p> <pre><code>// This structure will contain information about the file public struct SHFILEINFO { // Handle to the icon representing the file public IntPtr hIcon; // Index of the icon within the image list public int iIcon; // Various attributes of the file public uint dwAttributes; // Path to the file [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string szDisplayName; // File type [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] public string szTypeName; }; </code></pre> <p>The final preparation for the unmanaged code is to define the signature of SHGetFileInfo, which is located inside the popular Shell32.dll: </p> <pre><code>// The signature of SHGetFileInfo (located in Shell32.dll) [DllImport("Shell32.dll")] public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, uint uFlags); </code></pre> <p>Now that we have everything prepared, it's time to make the call to the function and display the icon that we retrieved. The object that will be retrieved is an Icon type (System.Drawing.Icon) but we want to display it in a PictureBox so we'll convert the Icon to a Bitmap using the ToBitmap() method. </p> <p>But first of all there are 3 controls you need to add to the form, a Button btnExtract that has "Extract Icon" for its Text property, picIconSmall which is a PictureBox and a picIconLarge which is also a PictureBox. That's because we will get two icons sizes. Now double click btnExtract in Visual Studio's Design view and you'll get to its Click event. Inside it is the rest of the code:</p> <pre><code>private void btnExtract_Click(object sender, EventArgs e) { // Will store a handle to the small icon IntPtr hImgSmall; // Will store a handle to the large icon IntPtr hImgLarge; SHFILEINFO shinfo = new SHFILEINFO(); // Open the file that we wish to extract the icon from if(openFile.ShowDialog() == DialogResult.OK) { // Store the file name string FileName = openFile.FileName; // Sore the icon in this myIcon object System.Drawing.Icon myIcon; // Get a handle to the small icon hImgSmall = SHGetFileInfo(FileName, 0, ref shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON); // Get the small icon from the handle myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon); // Display the small icon picIconSmall.Image = myIcon.ToBitmap(); // Get a handle to the large icon hImgLarge = SHGetFileInfo(FileName, 0, ref shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON); // Get the large icon from the handle myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon); // Display the large icon picIconLarge.Image = myIcon.ToBitmap(); } } </code></pre> <p>UPDATE: found even more info <a href="http://support.microsoft.com/kb/319350" rel="nofollow noreferrer">here</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload