【问题标题】:How can you access icons from a Multi-Icon (.ico) file using index in C#如何使用 C# 中的索引从多图标 (.ico) 文件中访问图标
【发布时间】:2012-02-03 06:00:48
【问题描述】:

我想使用 ico 文件中的第四张图片C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\VS2008ImageLibrary\1033\VS2008ImageLibrary\VS2008ImageLibrary\Objects\ico_format\WinVista\Hard_Drive.ico

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

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

【问题讨论】:

    标签: c# indexing icons


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2011-01-05
        • 2019-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多