tags:

views:

74

answers:

2

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.

+1  A: 
  • 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.
AMissico
I tried this, but VS2008 now stores toolstripbutton images as separate resource images instead.
NickAldwin
(rather than as the one imagestream resource)
NickAldwin
Try copying the ImageList to the new form, then add the image
AMissico
@NickAldwin: Updated answer. I have tested and it works.
AMissico
Thanks for your answer! I found a code solution at about the same time you updated, though.
NickAldwin
A: 

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.

NickAldwin