How to store background image into XML file

I added the background image for each product by the help of Item.Icon.Image command. But i cant save the image into XML file. we are using following for add the label into item. How to store image files.

t = new GoXmlBindingTransformer("item", new Item()); t.IdAttributeUsedForSharedObjects = true; // each Item gets a unique ID t.AddBinding("label", "Text"); t.AddBinding("iconname", "Icon.Name"); t.AddBinding("iconindex", "Icon.Index"); // assumes an ImgeList is set up for Item t.AddBinding("size", "Size"); t.AddBinding("location", "Location"); AddBrushBindings(t, "brush", "Back"); rw.AddTransformer(t);

Are your images coming from resource files, or on-disk (external to the application) files?

Image is load from External files. For each Type of product get individual images.

The code the way the sample is (and the way you have it) should work for ondisk files.
e.g.

I already added the icon for the individual product. After completed the planogram Design. The Designed planogram save the following type .plan, .bmp. On that time the design also write in the form of XML. Please refer the following code that is given by your developers.

//Helps to save the Planogram image into XML
public void StoreXml(FileStream ofile) { GoXmlWriter xw = new GoXmlWriter(); InitReaderWriter(xw); xw.Objects = this; xw.Generate(ofile); }
private static void InitReaderWriter(GoXmlReaderWriterBase rw) { GoXmlBindingTransformer.DefaultTracingEnabled = true; // for debugging, check your Output window (trace listener) GoXmlBindingTransformer t; t = new GoXmlBindingTransformer("planogram", new Planogram()); t.AddBinding("version", "Version", GoXmlBindingFlags.RethrowsExceptions); // let exception from Version setter propagate out t.AddBinding("name", "Name"); t.AddBinding("path", "Path"); rw.AddTransformer(t); t = new GoXmlBindingTransformer("item", new Item()); t.IdAttributeUsedForSharedObjects = true; // each Item gets a unique ID t.AddBinding("label", "Text"); t.AddBinding("iconname", "Icon.Name"); t.AddBinding("iconindex", "Icon.Index"); // assumes an ImgeList is set up for Item t.AddBinding("size", "Size"); t.AddBinding("location", "Location"); AddBrushBindings(t, "brush", "Back"); rw.AddTransformer(t); t = new GoXmlBindingTransformer("shelf", new Shelf()); t.HandlesChildren = true; // for Items dropped on shelf t.IdAttributeUsedForSharedObjects = true; // each shelf gets a unique ID t.AddBinding("label", "Text"); t.AddBinding("bounds", "Grid.Bounds"); rw.AddTransformer(t); t = new GoXmlBindingTransformer("rack", new Rack()); t.HandlesChildren = true; // for Items dropped on Rack t.IdAttributeUsedForSharedObjects = true; // each rack gets a unique ID t.AddBinding("label", "Text"); t.AddBinding("bounds", "Grid.Bounds"); rw.AddTransformer(t); t = new GoXmlBindingTransformer("gocomment", new GoComment()); t.IdAttributeUsedForSharedObjects = true; t.AddBinding("label", "Text"); t.AddBinding("location", "Location"); //AddFontBindings(t, "text", "Label"); AddBrushBindings(t, "brush", "Shape"); rw.AddTransformer(t); // GoBalloon is also a GoComment, so we don't need to repeat the label, location, fonts and id stuff here. t = new GoXmlBindingTransformer("goballoon", new GoBalloon()); t.AddBinding("unanchoredOffset", "UnanchoredOffset"); t.AddBinding("anchor", "Anchor"); t.AddBinding("reanchorable", "Reanchorable"); rw.AddTransformer(t); t = new GoXmlBindingTransformer("title", new Title()); t.IdAttributeUsedForSharedObjects = true; t.AddBinding("label", "Text"); t.AddBinding("location", "Location"); //AddFontBindings(t, "text", ""); rw.AddTransformer(t); }
The above code helps to save the product text, position, Background color etc.... But i cant save the Each product Background image(icon) into XML or .Plan files.

To extend GoXmlBindingTransformer to support a type it doesn’t know, you have to derive a new class and add the attributes support there.

Like this:
public class GoXmlBindingTransformerImage : GoXmlBindingTransformer {
public GoXmlBindingTransformerImage(String eltname, Object proto) : base (eltname, proto) {
}
public override void GenerateAttributes(object obj) {
base.GenerateAttributes(obj);
Item i = obj as Item;
if (i != null) {
MemoryStream stream = new MemoryStream();
i.Icon.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Close();
byte[] bdata = stream.GetBuffer();
string bits = Convert.ToBase64String(bdata);
WriteAttrVal("bits", bits);
}
}
public override void ConsumeAttributes(object obj) {
base.ConsumeAttributes(obj);
Item i = obj as Item;
if (i != null) {
String bits = StringAttr("bits", null);
if (bits != null) {
byte[] bytes2 = Convert.FromBase64String(bits);
MemoryStream stream = new MemoryStream(bytes2);
i.Icon.Image = Image.FromStream(stream);
stream.Close();
}
}
}
} // end of GoXmlBindingTransformerImage
then use GoXmlBindingTransformerImage as the trasnformer for the Item class.
Note that I use PNG as the storage format, that should be able to handle ico, bmp, etc.
One thing I haven't done here is attempt to save space in the xml by sharing common bitmaps.

Hi Jake,

Thank you for your code. Now its working really great... I got the great look and feel for my planogram design.