问题
I have a C++/CX component that can return an image as a stream (I could easily make it return an IBuffer, if that makes things easier). Currently, the component's return type is IRandomAccessStream and the actual stream is a InMemoryRandomAccessStream.
I want to use this stream as the source for an image in a HTML/JavaScript Windows 8 app. The problem is that I don't know how to shove this stream, or an array that I can create from it, into the image. I've tried a few things, but for example URL.createObjectURL deosn't take just an IRandomAccessStream, it requires an IRandomAccessStreamWithContentType. I've also tried to use a canvas and imagedata (http://www.w3schools.com/tags/canvas_createimagedata.asp).
My next step will probably be to try to wrap my stream in a IRandomAccessStreamWithContentType and see if I can make that work. Either in JavaScript or in my C++/CX control.
Any suggestions on how to go about this would be appreciated.
Thanks, Tomas
回答1:
I've done this in C# before not in C++, but the basics should be similar.
- Get the byte array from the stream
- Convert the byte array to Base64String (Convert.ToBase64String)
- Now you can use this string directly in the image tag:
<img src="data:image/png;base64,YOUR_STRING" />
来源:https://stackoverflow.com/questions/14162945/winrt-javascript-image-from-byte-array