views:

125

answers:

2

Let say I have application menus in a database with their icon images (binary data). I extract those menus with the icons being byte[] type. However if there's no icon set, then I would like to use default icon which comes not from database, but from xap (inside Resources folder). To display icons coming from database I use IConverter (byte[] to image), which is based on the code of the following question:

http://stackoverflow.com/questions/2445449/silverlight-4-0-how-to-convert-byte-to-image

To be able to use my byte[]-to-image IConverter, I would also like to convert my default icon to byte[], which comes from xap. How I can do that? The following question suggested to use WriteableBitmap class, but I don't know how to create WriteableBitMap from the xap source:

http://stackoverflow.com/questions/1958470/silverlight-image-to-byte

A: 

As your default icon is a resource, you can open it as a ResourceStream and just read it in as bytes.

Would that meet your requirements?

Enough already
+1  A: 

I may be miss understanding the question here (perhaps more details about your converter are required here), but if you converter class just returns an image based on its bytes, cant you just test for null bytes from the DB, then return your default image?

public class MyConveter : IConverter {
    public Image ConvertImage(byte[] bytes) {
        if (bytes == null) return GetDefaultImage();
        else return ConverterBytesToImage(bytes);
    }
}

this way you simply return an image as the method declaration, and the implementation handles the null bytes case.

Is this on the right track?

Mark
@Mark +1: This is a much neater solution than my suggestion (and hides away neatly behind the scenes). I shall add this to my own toolbox of tricks :)
Enough already
Thanks, I like abstraction and keeping this hidden from the levels that dont need to worry about it :)
Mark
Thank you, your solution was very intuitive.
synergetic