代码之家  ›  专栏  ›  技术社区  ›  NickAldwin

在资源(resx)中将图像添加到Base64编码的ImageStream

  •  5
  • NickAldwin  · 技术社区  · 15 年前

    我在做一个老项目,更新它。程序的一部分有一个工具栏,上面有许多按钮,每个按钮上都有一个图像。我发现图像存储在表单的resx中的Base64编码的imagestream中,并按如下方式访问:

    System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
    ...
    this.imageList1 = new System.Windows.Forms.ImageList(this.components);
    ...
    this.toolStrip1.ImageList = this.imageList1;
    ...
    this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
    ...
    this.toolStripButton1.ImageIndex = 0;  //There are 41 images, so this can be between 0 and 40
    

    我需要添加一个新的图像另一个按钮。如何将图像添加到此流?

    我不能使用设计器,因为它在我加载表单时就崩溃了(我相信是因为它使用了带有不安全代码的自定义组件)。

    我总是可以添加一个与流分离的新图像资源,但这会使一个按钮不同,因此会产生一致性问题,导致以后的维护问题。所以我想知道有没有 不管怎样

    2 回复  |  直到 15 年前
        1
  •  6
  •   AMissico    15 年前
    • 添加ImageList组件。
    • 添加任意图像以生成“imagestream”。
    • 打开旧resx并复制“value”元素。
    • 打开新resx并粘贴value元素。
    • 打开新窗体。
    • 保存新表单。
    • 打开新窗体的resx文件。
    • 打开旧窗体的resx文件。
    • 粘贴新的值元素。
        2
  •  7
  •   NickAldwin    15 年前

    我找到了一种使用代码的方法:

    imageList1.Images.Add( NEWIMAGE );
    ResXResourceWriter writer = new ResXResourceWriter("newresource.resx");
    writer.AddResource("imageList1.ImageStream",imageList1.ImageStream);
    writer.Generate();
    writer.Close();
    writer.Dispose();