Exegesis
Exegesis

Reputation: 1088

WP7 Converting an ASCII String to an Image

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();
            bitmapImage.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.

Upvotes: 1

Views: 1961

Answers (1)

AnthonyWJones
AnthonyWJones

Reputation: 189505

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.

Upvotes: 3

Related Questions