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 Windows Photo Viewer, shows me 13 different icons.
I have dumped this ico in a resource file, how can I retreive the required icon using index.
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 );
}
You'll need to manually parse through the .ico file grabbing information from the header (see here for a layout of the .ico file type).
There's an open source project on vbAccelerator (don't worry it's actually c# code, not VB) that uses the Win32 API to extract icons from resources (exe, dll and even ico, which is what you are looking to do). You could either use that code or go through it for an good idea of how it is done. The source code can be browsed here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With