C#从字符串转换为图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17348042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting from string to Image in C#
提问by user2528880
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
我正在尝试将 Unicode 字符串转换为 C# 中的图像。每次我运行它时,我都会在这一行出现错误
Image image = Image.FromStream(ms, true, true);
that says: ArgumentException was unhandled by user code. Parameter is not valid. Any ideas why this is happening? Below is the rest of the function.
上面写着: 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;
}
回答by Jon Benedicto
Take out your call that writes to the MemoryStream. The constructor call that accepts a byte array automatically puts the contents of the byte array into the stream. Otherwise your stream contains 2 copies of the raw data. In addition, the call to Write will leave the stream's position at the end of stream, so there is no data that the FromStream call can read.
取出写入 MemoryStream 的调用。接受字节数组的构造函数调用会自动将字节数组的内容放入流中。否则,您的流包含原始数据的 2 个副本。另外,Write 的调用会将流的位置留在流的末尾,因此 FromStream 调用无法读取任何数据。
So it would be:
所以它会是:
public Image stringToImage(string inputString)
{
byte[] imageBytes = Encoding.Unicode.GetBytes(inputString);
// Don't need to use the constructor that takes the starting offset and length
// as we're using the whole byte array.
MemoryStream ms = new MemoryStream(imageBytes);
Image image = Image.FromStream(ms, true, true);
return image;
}
回答by Jeff Foster
Unicode doesn't encode all possible byte sequences that you'll need to represent an image.
Unicode 不会对表示图像所需的所有可能的字节序列进行编码。
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.
byte[]-> String->byte[]是一种转换,它不适用于许多给定的字节序列。您必须始终使用 byte[]。
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.
例如,如果您读取字节,将它们转换为 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.
代码点 U+D800 到 U+DFFF[编辑] Unicode 标准永久保留这些代码点值用于引导和尾随代理的 UTF-16 编码,它们永远不会被分配一个字符,所以应该没有理由编码他们。官方 Unicode 标准说所有 UTF 格式,包括 UTF-16,都不能对这些代码点进行编码。
回答by r.mirzojonov
May this can help you:
可能这可以帮助您:
public Bitmap stringToImage(string inputString)
{
byte[] imageBytes = Encoding.Unicode.GetBytes(inputString);
using (MemoryStream ms = new MemoryStream(imageBytes))
{
return new Bitmap(ms);
}
}
回答by terrybozzio
With string you might get loss of data,i will just post example on converting image to byte array and array to image again,and after image to byte array,to string and back,without loss of data.
使用字符串,您可能会丢失数据,我将仅发布有关将图像转换为字节数组和数组再次转换为图像的示例,以及将图像转换为字节数组、字符串并返回,而不会丢失数据。
MemoryStream ms = new MemoryStream();
Image.FromFile(@"C:\..\..\..\img.jpg").Save(ms,ImageFormat.Jpeg);
byte[] bytes = ms.ToArray();
MemoryStream ms1 = new MemoryStream(bytes);
Image NewImage = Image.FromStream(ms1);
NewImage.Save(@"C:\..\..\..\img1.jpg");
try this out and it might help you produce what you need.
试试这个,它可能会帮助你生产你需要的东西。
trying to convert to string and back,better use base64.
尝试转换为字符串并返回,最好使用 base64。
MemoryStream ms = new MemoryStream();
Image.FromFile(@"C:\..\..\..\img.jpg").Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = ms.ToArray();
string byteString = Convert.ToBase64String(bytes);
byte[] NewBytes = Convert.FromBase64String(byteString);
MemoryStream ms1 = new MemoryStream(NewBytes);
Image NewImage = Image.FromStream(ms1);
This should give you the outcome you need.
这应该会给你你需要的结果。
MemoryStream ms = new MemoryStream();
Image.FromFile(@"C:\..\..\..\img.jpg").Save(ms,ImageFormat.Jpeg);
byte[] bytes = ms.ToArray();
string byteString = Convert.ToBase64String(bytes);
then when you pass this string into your method...
然后当你将这个字符串传递到你的方法中时......
public Image stringToImage(string inputString)
{
byte[] NewBytes = Convert.FromBase64String(inputString);
MemoryStream ms1 = new MemoryStream(NewBytes);
Image NewImage = Image.FromStream(ms1);
return NewImage;
}
回答by antiduh
You're getting an image as a string, out of ldap? I'm pretty sure if that was true, the string would actually be base64 encoded, and in that case contains bytes that represent actual characters, and not image data.
你从 ldap 中得到一个字符串形式的图像?我很确定如果这是真的,字符串实际上是 base64 编码的,在这种情况下,包含代表实际字符的字节,而不是图像数据。
Could you post a snippit of the string you're getting?
你能发布一个你得到的字符串的片段吗?
If it's true, you need to take the string and turn it a byte[] by un-base64'ing it, and then use the byte array to make the image. Combined with @JonBenedicto's code:
如果是真的,你需要取字符串并通过 un-base64 将它变成一个字节 [],然后使用字节数组来制作图像。结合@JonBenedicto 的代码:
public Image stringToImage(string inputString)
{
byte[] imageBytes = Convert.FromBase64String(inputString);
MemoryStream ms = new MemoryStream(imageBytes);
Image image = Image.FromStream(ms, true, true);
return image;
}

