I am creating an object server-side which is to have all of the data necessary to represent the object client-side. One of the properties I would like my client-object to have is the its icon.
Currently, I have the following code. It hasn’t been ran yet, I just wanted to discuss some things with S.O:
public class ComponentDiagramPolygonChild
{
IconServicesSoapClient IconServicesProxy = new IconServicesSoapClient("IconServicesSoap");
public string Name { get; set; }
public byte[] Icon { get; set; }
public ComponentDiagramPolygonChild(PlanViewComponent planViewComponent)
{
Name = planViewComponent.Name;
//TODO: Can icon ever return null here?
Icon = IconServicesProxy.GetIconByID(planViewComponent.Icon).Image;
planViewComponent.
}
}
It seems to me that I have two options available:
- Store the ID of the Icon as the property instead of byte[].
- Store the Icon as a byte[].
If I use the former suggestion I will need to go out to the server and request the image data by passing to the server the icon’s ID. I believe this would be a more simplistic approach — I would just return a memory stream housing the image and load that stream in the desired location. The negative aspect of this is multiple hits on the server which seem to be completely unnecessary.
If I use the latter suggestion I will be passing a (significantly?) larger amount of data across the network. I am unsure if byte arrays are able to be JSONized properly. I am unsure of how to take a jsonized byte[] and convert it into image data.
Does anyone have any experience with this situation? How should I tackle this? Thanks.
EDIT: After doing some reading it looks like the former option is my best bet.
I think from a technical and programming point of view, it would be cool to pass the data image back.
You could do that with a base64 encoded version of the image, and most modern browsers will support that. You could also do it with the HTML5 canvas object. Documentation is available on the MDN.
However, from a practical point of view, the most maintainable solution is to use an ID and let it make a separate HTTP request. No HTML 5 required, supported in pretty much every browser in use, and you can reuse existing image delivery systems.
If your server is tuned, it will be OK to have that extra HTTP request for the image, and it will be much less CPU load on the server if you are doing a lot of these images.