[英]Add Image to Base64 Encoded ImageStream in Resources (resx)
I'm working on an older project, updating it. 我正在研究一个较旧的项目,更新它。 Part of the program has a toolstrip with many buttons on it, each with an image. 该程序的一部分有一个工具条,上面有许多按钮,每个按钮都有一个图像。 I found that the images are stored in a Base64 encoded imagestream in the resx for the form and accessed as such: 我发现图像存储在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
I need to add another button with a new image. 我需要添加另一个带有新图像的按钮。 How can I add an image to this stream? 如何将图像添加到此流?
I cannot use the designer, as it crashes as soon as I load the form (I believe because it uses a custom component with unsafe code). 我无法使用设计器,因为它在加载表单时崩溃(我相信因为它使用带有不安全代码的自定义组件)。
I could always just add a new image resource separate from the stream, but that would make that one button different and thus it would create problems with consistency, causing upkeep problems later. 我总是可以添加一个与流分开的新图像资源,但这会使一个按钮不同,因此会产生一致性问题,从而导致以后出现维护问题。 So I'm wondering if there is any way for me to edit the imagestream. 所以我想知道我是否有办法编辑图像流。 I can access the raw base64 string, but I have no idea where to go from here. 我可以访问原始base64字符串,但我不知道从哪里开始。
I found a way to do this using code: 我找到了一种使用代码执行此操作的方法:
imageList1.Images.Add( NEWIMAGE );
ResXResourceWriter writer = new ResXResourceWriter("newresource.resx");
writer.AddResource("imageList1.ImageStream",imageList1.ImageStream);
writer.Generate();
writer.Close();
writer.Dispose();
The code will write the updated ImageStream to the new resource file. 代码将更新的ImageStream写入新的资源文件。 I can then copy it to my current resource file. 然后我可以将其复制到我当前的资源文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.