C# 如何使用C中的索引从多图标(.ico)文件访问图标#

C# 如何使用C中的索引从多图标(.ico)文件访问图标#,c#,indexing,icons,C#,Indexing,Icons,我想使用ico文件中的第四个图像:C:\ProgramFiles(x86)\Microsoft Visual Studio 9.0\Common7\VS2008ImageLibrary\1033\VS2008ImageLibrary\VS2008ImageLibrary\Objects\ico\u format\WinVista\Hard\u Drive.ico 如果我使用Windows照片查看器看到此图标,将显示13个不同的图标 我已将此ico转储到资源文件中,如何使用索引检索所需的图标。您需

我想使用ico文件中的第四个图像:
C:\ProgramFiles(x86)\Microsoft Visual Studio 9.0\Common7\VS2008ImageLibrary\1033\VS2008ImageLibrary\VS2008ImageLibrary\Objects\ico\u format\WinVista\Hard\u Drive.ico

如果我使用Windows照片查看器看到此图标,将显示13个不同的图标


我已将此ico转储到资源文件中,如何使用索引检索所需的图标。

您需要手动解析.ico文件,从标题中获取信息(有关.ico文件类型的布局,请参阅)


vbAccelerator上有一个开源软件(不用担心,它实际上是c代码,而不是VB),它使用Win32 API从资源(exe、dll甚至ico)中提取图标,这正是您想要做的。您可以使用该代码,也可以浏览该代码以了解如何完成。可以浏览源代码。

在WPF中,可以执行以下操作:

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 );
}