2012-01-05 16 views
8

Tôi muốn sử dụng hình ảnh 4 từ file ico: C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\VS2008ImageLibrary\1033\VS2008ImageLibrary\VS2008ImageLibrary\Objects\ico_format\WinVista\Hard_Drive.icoLàm thế nào bạn có thể truy cập vào các biểu tượng từ một (.ico) tập tin đa-Biểu tượng sử dụng chỉ mục trong C#

Nếu tôi nhìn thấy biểu tượng này bằng Windows Photo Viewer , cho tôi thấy 13 biểu tượng khác nhau.

Tôi đã bán ico này trong tệp tài nguyên, làm cách nào tôi có thể truy xuất biểu tượng bắt buộc bằng chỉ mục.

Trả lời

6

Trong WPF, bạn có thể làm một cái gì đó như thế này:

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

Bạn cần phải phân tích cú pháp thông tin lấy thông tin tệp .ico từ tiêu đề (xem here để bố cục loại tệp .ico).

Có một mã nguồn mở project trên vbAccelerator (đừng lo lắng nó thực sự C# mã, không VB) có sử dụng Win32 API để trích xuất các biểu tượng từ các nguồn lực (exe, dll và thậm chí ico, đó là những gì bạn đang tìm kiếm để làm). Bạn có thể sử dụng mã đó hoặc xem xét nó để biết cách thực hiện nó. Mã nguồn có thể được duyệt here.

Các vấn đề liên quan