我想使用ico文件中的4映像:C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\VS2008ImageLibrary\1033\VS2008ImageLibrary\VS2008ImageLibrary\Objects\ico_format\WinVista\Hard_Drive.ico
如果我看到这个图标使用,给我显示13个不同的图标。
我已经将此ico转储到一个资源文件中,如何使用索引恢复所需的图标。
发布于 2012-01-05 12:37:13
在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 );
}发布于 2012-01-05 12:13:59
您需要手动解析.ico文件,从头部获取信息(有关.ico文件类型的布局,请参见这里 )。
有一个开源的项目 on vbAccelerator (不用担心,实际上是c#代码,而不是VB),它使用Win32 API从资源中提取图标(exe、dll甚至ico,这就是你想要做的)。您可以使用该代码,也可以通过它来了解它是如何完成的。源代码可以浏览这里。
https://stackoverflow.com/questions/8742047
复制相似问题