Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Image to Base64 Encoded ImageStream in Resources (resx)

Tags:

c#

.net

resources

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:

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.

like image 945
NickAldwin Avatar asked Jun 14 '10 16:06

NickAldwin


People also ask

How do you insert an image into RESX?

Create a new resource, say, Images. resx in in some directory. Have some ready to use JPEG file. Open the created resource and use "Add Resource" -> "Add Existing File", add your file.

How do I open a RESX file?

Navigate to resources from File StructureOpen a . resx file in the XML (Text) editor. Press Ctrl+Alt+F or choose ReSharper | Windows | File Structure from the main menu . Alternatively, you can press Ctrl+Shift+A , start typing the command name in the popup, and then choose it there.


2 Answers

  • Create another form.
  • Add an ImageList component.
  • Add an arbitrary image in order to generate an "imagestream".
  • Open old resx and copy the "value" element.
  • Open new resx and paste value element.
  • Open new form.
  • Add images as needed.
  • Save new form.
  • Open new form's resx file.
  • Copy value element.
  • Open old form's resx file.
  • Paste in new value element.
like image 150
AMissico Avatar answered Sep 20 '22 13:09

AMissico


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. I can then copy it to my current resource file.

like image 27
NickAldwin Avatar answered Sep 20 '22 13:09

NickAldwin