You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取文件中存储的图标数量?ReturnIcon函数扩展咨询

Should You Integrate Icon Counting into ReturnIcon or Create a New Function?

Great question! The answer depends on how you’re already using the ReturnIcon function and your future workflow needs. Let’s break down both approaches with practical examples:

If ReturnIcon is already used across your project, adding a dedicated icon-counting function is the safest bet—it avoids breaking existing code that expects a Bitmap return type. This approach keeps responsibilities clear: one function extracts icons, the other counts them.

Here’s a Windows API-based example (using P/Invoke, standard for handling Windows icon resources):

using System;
using System.Runtime.InteropServices;
using System.Drawing;

public static class IconHelper
{
    // Your existing ReturnIcon function
    public static Bitmap ReturnIcon(string filePath, int iconIndex = 0)
    {
        // Your current logic to extract and convert the icon to Bitmap
        // (omitted for brevity—keep your existing implementation here)
    }

    // New function to get total icon count from a file
    [DllImport("shell32.dll", CharSet = CharSet.Auto)]
    private static extern uint ExtractIconEx(string lpszFile, int nIconIndex, IntPtr[] phiconLarge, IntPtr[] phiconSmall, uint nIcons);

    public static int GetIconCount(string filePath)
    {
        // Pass -1 as the icon index to retrieve the total count
        uint totalIcons = ExtractIconEx(filePath, -1, null, null, 0);
        return (int)totalIcons;
    }
}

How to Use It:

string targetFile = "C:\\example\\app.exe";
int iconCount = IconHelper.GetIconCount(targetFile);
Console.WriteLine($"Total icons in file: {iconCount}");

// Extract a specific icon using your existing function
Bitmap firstIcon = IconHelper.ReturnIcon(targetFile, 0);

Option 2: Integrate Counting into ReturnIcon (For Unified Workflows)

If you often need both the icon count and the extracted icons together, modifying ReturnIcon to return a combined result makes sense. This reduces redundant file reads and streamlines workflows where you need both pieces of data.

Example with a custom result class:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Drawing;

public class IconExtractionResult
{
    public int TotalIconCount { get; set; }
    public List<Bitmap> ExtractedIcons { get; set; } = new List<Bitmap>();
}

public static class IconHelper
{
    [DllImport("shell32.dll", CharSet = CharSet.Auto)]
    private static extern uint ExtractIconEx(string lpszFile, int nIconIndex, IntPtr[] phiconLarge, IntPtr[] phiconSmall, uint nIcons);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern bool DestroyIcon(IntPtr hIcon);

    public static IconExtractionResult ReturnIcon(string filePath, bool extractAllIcons = false)
    {
        var result = new IconExtractionResult();
        
        // First get the total icon count
        uint totalIcons = ExtractIconEx(filePath, -1, null, null, 0);
        result.TotalIconCount = (int)totalIcons;

        if (totalIcons == 0) return result;

        if (extractAllIcons)
        {
            // Extract every icon in the file
            IntPtr[] iconHandles = new IntPtr[totalIcons];
            ExtractIconEx(filePath, 0, iconHandles, null, totalIcons);

            foreach (IntPtr handle in iconHandles)
            {
                using (Icon icon = Icon.FromHandle(handle))
                {
                    result.ExtractedIcons.Add(icon.ToBitmap());
                }
                DestroyIcon(handle); // Clean up unmanaged resources
            }
        }
        else
        {
            // Keep original behavior: extract just the first icon
            IntPtr[] singleIconHandle = new IntPtr[1];
            ExtractIconEx(filePath, 0, singleIconHandle, null, 1);
            
            using (Icon icon = Icon.FromHandle(singleIconHandle[0]))
            {
                result.ExtractedIcons.Add(icon.ToBitmap());
            }
            DestroyIcon(singleIconHandle[0]);
        }

        return result;
    }
}

How to Use This Modified Version:

string targetFile = "C:\\example\\library.dll";
var extractionResult = IconHelper.ReturnIcon(targetFile, extractAllIcons: true);

Console.WriteLine($"Total icons: {extractionResult.TotalIconCount}");
foreach (Bitmap iconBmp in extractionResult.ExtractedIcons)
{
    // Use each bitmap (e.g., display in UI, save to disk)
}

Which Approach to Pick?

  • Separate function: Choose this if you want to preserve backward compatibility and keep your code’s responsibilities focused.
  • Integrated function: Choose this if you frequently need both the count and icons together, and don’t mind updating existing calls to handle the new return type.

内容的提问来源于stack exchange,提问作者Simos Sigma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:42:55