How can you access the icons from a Multi-Icon (.ico) file using an index in C #

I want to use the 4th image from the ico file: C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\VS2008ImageLibrary\1033\VS2008ImageLibrary\VS2008ImageLibrary\Objects\ico_format\WinVista\Hard_Drive.ico

If I see this icon using the Windows Photo Viewer, 13 different icons are displayed.

I dumped this file in the resource file, how can I restore the required icon using the index.

+8
c # indexing icons
source share
2 answers

In WPF, you can do something like this:

 Stream iconStream = new FileStream ( @"C:\yourfilename.ico", FileMode.Open ); IconBitmapDecoder decoder = new IconBitmapDecoder ( iconStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None ); // loop through images inside the file foreach ( var item in decoder.Frames ) { //Do whatever you want to do with the single images inside the file this.panel.Children.Add ( new Image () { Source = item } ); } // or just get exactly the 4th image: var frame = decoder.Frames[3]; // save file as PNG BitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(frame); using ( Stream saveStream = new FileStream ( @"C:\target.png", FileMode.Create )) { encoder.Save( saveStream ); } 
+6
source share

You will need to manually parse the .ico file, capturing the information from the header (see here for the layout of the .ico file type).

There is an open project on vbAccelerator (do not worry, this is C # code, not VB) that uses the Win32 API to extract icons from resources (exe, dll and even ico what you are looking for). You can use this code or go through it to understand how this is done. The source code can be viewed here .

+5
source share

All Articles