简体   繁体   English

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

[英]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字符串,但我不知道从哪里开始。

  • Create another form. 创建另一个表单。
  • Add an ImageList component. 添加ImageList组件。
  • Add an arbitrary image in order to generate an "imagestream". 添加任意图像以生成“图像流”。
  • Open old resx and copy the "value" element. 打开旧的resx并复制“value”元素。
  • Open new resx and paste value element. 打开新的resx和paste值元素。
  • Open new form. 打开新表格。
  • Add images as needed. 根据需要添加图像。
  • Save new form. 保存新表格。
  • Open new form's resx file. 打开新表单的resx文件。
  • Copy value element. 复制值元素。
  • Open old form's resx file. 打开旧表单的resx文件。
  • Paste in new value element. 粘贴新值元素。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM