C#Toolbar图像集合编辑器



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

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

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

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

这源于对以下问题的回答:如何在VS2005中从图像列表导出图像?

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

如果有人有更好的想法或一个小应用程序,只使用资源文件从工具栏中检索图像,那将不胜感激。我不会标记为答案,因为这更多的是一种变通方法。

我使用这种方法:

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

我需要从控件的私有ImageList成员恢复图像。我使用了以下代码(对不起,这是VB,但s/b很容易重构)

    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

最新更新