我正在研究一个较旧的项目,更新它.该程序的一部分有一个工具条,上面有许多按钮,每个按钮都有一个图像.我发现图像存储在resx中的Base64编码图像流中,用于表单并按如下方式访问:
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
Run Code Online (Sandbox Code Playgroud)
我需要添加另一个带有新图像的按钮.如何将图像添加到此流?
我无法使用设计器,因为它在加载表单时崩溃(我相信因为它使用带有不安全代码的自定义组件).
我总是可以添加一个与流分开的新图像资源,但这会使一个按钮不同,因此会产生一致性问题,从而导致以后出现维护问题.所以我想知道我是否有办法编辑图像流.我可以访问原始base64字符串,但我不知道从哪里开始.
我找到了一种使用代码执行此操作的方法:
imageList1.Images.Add( NEWIMAGE );
ResXResourceWriter writer = new ResXResourceWriter("newresource.resx");
writer.AddResource("imageList1.ImageStream",imageList1.ImageStream);
writer.Generate();
writer.Close();
writer.Dispose();
Run Code Online (Sandbox Code Playgroud)
代码将更新的ImageStream写入新的资源文件.然后我可以将其复制到我当前的资源文件中.
| 归档时间: |
|
| 查看次数: |
6843 次 |
| 最近记录: |