gpt4 book ai didi

c# - 在 C# 中将字符串转换为图像

转载 作者:太空狗 更新时间:2023-10-29 17:40:20 24 4
gpt4 key购买 nike

我正在尝试将 Unicode 字符串转换为 C# 中的图像。每次运行它时,都会在这一行出现错误

Image image = Image.FromStream(ms, true, true);

表示:ArgumentException 未被用户代码处理。参数无效。任何想法为什么会这样?下面是函数的其余部分。

public Image stringToImage(string inputString)
{
byte[] imageBytes = Encoding.Unicode.GetBytes(inputString);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true, true);

return image;
}

最佳答案

Unicode 不会对表示图像所需的所有可能字节序列进行编码。

byte[] -> String -> byte[] 是一种转换,它不适用于许多给定的字节序列.您必须自始至终使用 byte[]。

例如,如果您读取字节,将它们转换为 UTF-16,则字节序列可能会因无效而被丢弃。这是来自 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.

关于c# - 在 C# 中将字符串转换为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17348042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com