tags:

views:

69

answers:

2

I am developing smart device application in C#. In that I am calling the web services. The web service method return google map. The return type of the method is object. The object contains the image in byte format. The object conatins the image in base64binary format. I need to display the actual image in my application. What type of casting I need to do to display the image. Can you provide me the code or any link through which I can resolve the above issue?

+3  A: 

If you have a string where the bytes of an image are indeed in base64 encoding then you want to call the static FromBase64String method on the Convert class to get an array of bytes:

byte[] bytes = Convert.FromBase64String("base 64 string");

From there, you want to create a new instance of the MemoryStream class with the bytes:

MemoryStream ms = new MemoryStream(bytes);

Finally, the last call you would make is to the static FromStream method on the Image class to get an Image you can work with:

Image image = Image.FromStream(ms);

Note in the Remarks section for the documentation for the FromStream method on the Image class, you have to keep the Stream instance (from which MemoryStream derives) open for as long as you use the Image.

Also note that while the implementation of MemoryStream does not use unmanaged resources and doesn't necessarily need a call to Dispose, that's coding against the implementation details, as opposed to the contract details, which is defined by the Stream class, which indicates you should call Dispose when done with it.

The reason you would want to do this is in the event the Stream for the image is a different subclass. For example, if a FileStream instance is used, you must call Dispose on it when you are done with it (along with the Dispose method on the Image class, as well).

casperOne
A: 

You should be able to put the byte[] into a MemoryStream to create the image like below

byte[] yourImage;
MemoryStream ms = new MemoryStream(image);
Image.FromStream(ms);
sadboy