Converting from string to Image in C#

前端 未结 5 2086
生来不讨喜
生来不讨喜 2021-02-20 06:22

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);
         


        
5条回答
  •  伪装坚强ぢ
    2021-02-20 06:53

    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.

提交回复
热议问题