I am trying to convert an ASCII string to an ImageSource like so in WP7:
string imageString = "%PNG image data here in ASCII"; byte[] imageBytes = new byte[sizeOfImage]; System.Text.Encoding.UTF8.GetBytes(imageString.ToCharArray(), 0, imageString.ToCharArray().Length - 1, imageBytes, 0); MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); // Convert byte[] to Image ms.Write(imageBytes, 0, imageBytes.Length); BitmapImage bitmapImage = new BitmapImage(); bit开发者_StackOverflowmapImage.SetSource(ms);
But there is no Encoding.ASCII in WP7 so obviously on the "Encoding.UTF" line it fails because WP7 only has that encoding. Is there any workaround for this?
Note: the ASCII string represents a PNG file.
I suspect that what you really want to be doing is this:-
byte[] imageBytes = Convert.FromBase64String(imageString);
However Lasse is right. Why on earth would you want to do this? Just add the png to the Xap as Content. The whole becomes something like:-
BitmapImage bitmapImage = new BitmapImage(new Uri("/Assets/yourfile.png", UriKind.Relative));
Where Assets is a folder you create in your project to store such things.
精彩评论