I am trying to convert a Unicode string to an image in C#. Each time I run it I get an error on this line
Image image = Image.FromStream(ms, true, true);
Unicode doesn't encode all possible byte sequences that you'll need to represent an image.
byte[]
-> String
-> byte[]
is a transformation that just won't work for many given sequences of bytes. You'll have to use a byte[] throughout.
For example, if you read the bytes, convert them to UTF-16 then it's possible that byte sequences will be discarded as invalid. Here's an example of an invalid byte sequence from UTF-16.
Code points U+D800 to U+DFFF[edit] The Unicode standard permanently reserves these code point values for UTF-16 encoding of the lead and trail surrogates, and they will never be assigned a character, so there should be no reason to encode them. The official Unicode standard says that all UTF forms, including UTF-16, cannot encode these code points.