【问题标题】:C# Toolbar Image Collection EditorC# 工具栏图像集合编辑器
【发布时间】:2026-02-14 23:40:01
【问题描述】:

我有一个带有工具栏和图像集合的应用程序。问题是我没有原始图像,我需要使用一些相同的按钮创建另一个工具栏。有没有办法将工具栏中的图像集合保存到文件中?

我尝试从资源文件中提取图像,但我不知道图像存储在哪个文件中。

【问题讨论】:

  • 嗨@UweKeim。对不起,我忘了指出。是的,它是 windows 窗体,Windows7 中的 VS2010

标签: c# winforms image collections toolbar


【解决方案1】:

虽然我没有找到问题的答案,但我还是设法通过读取工具栏图像列表并根据给定的图像键将每个图像保存到文件来获取图像。

for (int x = 0; x < this.imageListToolbar3small.Images.Count; ++x)
        {
            Image temp = this.imageListToolbar.Images[x];
            temp.Save(this.imageListToolbar.Images.Keys[x] + ".png");
        }

这来自对这个问题的回答:How to Export Images from an Image List in VS2005?

我刚刚在 InitializeComponent 调用之后添加了代码,并在调试模式下保存了所有图像。我不需要运行完整的应用程序。

如果有人确实有更好的想法或小型应用程序来仅使用资源文件从工具栏中检索图像,那将不胜感激。我不会将其标记为答案,因为它更像是一种解决方法。

【讨论】:

    【解决方案2】:

    我使用这种方法:

    foreach (ToolBarButton b in toolBar.Buttons)
    {
      //can be negative, for separators, because separators don't have images
      if (b.ImageIndex >= 0)
      {
        Image i = toolBar.ImageList.Images[b.ImageIndex];
        i.Save(b.ImageIndex + ".png");
      }
    }
    

    【讨论】:

      【解决方案3】:

      我需要从控件的私有 ImageList 成员中恢复图像。我使用了以下代码(抱歉,它是 VB,但易于重构)

          Dim cntrl = New TheClassWithThePrivateImageList
          Dim pi As Reflection.PropertyInfo, iml As System.Windows.Forms.ImageList, propName = "ThePropertyName"
          pi = cntrl.GetType.GetProperty(propName, Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
          iml = CType(pi.GetValue(cntrl), System.Windows.Forms.ImageList)
          For Each key In iml.Images.Keys
              Dim image As Drawing.Image = iml.Images.Item(key)
              image.Save($"{propName}_{key}")
          Next
      

      【讨论】: