将 byte[] 转换为 Bitmap c# 时出现 ArgumentException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/503298/
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
ArgumentException when converting byte[] to Bitmap c#
提问by
I'm trying to convert a byte array to a bitmap but it always shows me:
我正在尝试将字节数组转换为位图,但它总是向我显示:
System.ArgumentException: Parameter is not valid.
System.ArgumentException: 参数无效。
My code is as follows:
我的代码如下:
I'm passing the bytes through a webservice with:
我正在通过网络服务传递字节:
string DecodedString = string.Empty;
DecodedString = System.Text.Encoding.GetEncoding(1251).GetString(bytes);
sResult = sResult + "<Photo>" +XmlConvert.EncodeName(DecodedString) + "</Photo>";
and in my webPage:
在我的网页中:
byte[] bytes = (Byte[])System.Text.Encoding.GetEncoding(1251).GetBytes(XmlConvert.DecodeName(xDocument.SelectSingleNode("Response/Images/Photo").InnerText));
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
System.Drawing.Bitmap b = new System.Drawing.Bitmap(ms);//(System.Drawing.Image.FromStream(ms));
回答by configurator
Try passing the string as a Base64:
尝试将字符串作为 Base64 传递:
string DecodedString = string.Empty;
DecodedString = System.Convert.ToBase64String(bytes)
sResult = sResult + "<Photo>" +XmlConvert.EncodeName(DecodedString) + "</Photo>";
...
...
byte[] bytes = System.Convert.FromBase64String(xDocument.SelectSingleNode("Response/Images/Photo").InnerText);
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
System.Drawing.Bitmap b = System.Drawing.Image.FromStream(ms);
You also won't need to use XmlConvert to encode/decode the string.
您也不需要使用 XmlConvert 来编码/解码字符串。
回答by vt100
I had a similar problem recently, but using Silverlight. I ended up needing to create a Custom HTTP Handler in order to pass the byte[] that defined the image back as a stream.
我最近遇到了类似的问题,但使用的是 Silverlight。我最终需要创建一个自定义 HTTP 处理程序,以便将定义图像的字节 [] 作为流传回。
See http://www.dotnetcurry.com/ShowArticle.aspx?ID=220
见http://www.dotnetcurry.com/ShowArticle.aspx?ID=220
Edit: This allows you to avoid worrying about XML encoding, and passes the image back in Binary form... YMMV
编辑:这使您可以避免担心 XML 编码,并将图像以二进制形式传回...... YMMV
回答by Phaedrus
Try this:
尝试这个:
byte[] bytes = System.Convert.FromBase64String(xDocument.SelectSingleNode("Response/Images/Photo").InnerText);
System.Drawing.ImageConverter imageConverter = new System.Drawing.ImageConverter();
Image image = imageConverter.ConvertFrom(bytes) as Image;
System.Drawing.Bitmap b = new System.Drawing.BitMap(image);
EDIT
编辑
Take a look at this:
看看这个:
回答by configurator
According to MSDN:
根据MSDN:
ArgumentException - The stream does not have a valid image format
ArgumentException - 流没有有效的图像格式
I believe your problem is in the original byte[]
array you are passing to the web service.
According to one of your comments, you did:
我相信您的问题出在byte[]
您传递给 Web 服务的原始数组中。根据您的评论之一,您做了:
System.IO.FileStream fs = new System.IO.FileStream(sPath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
int streamLength = Convert.ToInt32(fs.Length);
bytes = new byte[streamLength];
fs.Read(bytes, 0, streamLength);
fs.Read
returns the number of bytes that have been read into the byte array; it doesn't always read the entire file!
Try using the StreamFile
method from http://www.dotnetspider.com/resources/4515-convert-file-into-byte-array.aspx. (First result of Google search)
fs.Read
返回已读入字节数组的字节数;它并不总是读取整个文件!
尝试使用http://www.dotnetspider.com/resources/4515-convert-file-into-byte-array.aspx 中的StreamFile
方法。(谷歌搜索的第一个结果)
回答by configurator
I did it, with the help of all of you, here is my page code
我做到了,在大家的帮助下,这是我的页面代码
byte[] bytes = System.Convert.FromBase64String(xDocument.SelectSingleNode("Response/Images/Photo").InnerText);
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
System.Drawing.Bitmap b = new System.Drawing.Bitmap(ms); //(Bitmap)System.Drawing.Image.FromStream(ms);
System.Drawing.Imaging.FrameDimension frameDim;
frameDim = new System.Drawing.Imaging.FrameDimension(b.FrameDimensionsList[0]);
int NumberOfFrames = b.GetFrameCount(frameDim);
string[] paths = new string[NumberOfFrames];
for (int i = 0; i < NumberOfFrames; i++)
{
b.SelectActiveFrame(frameDim, i);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(b);
paths[i] = imagePathfile.Remove(imagePathfile.Length - 4, 4) + i.ToString() + ".gif";
bmp.Save(paths[i], System.Drawing.Imaging.ImageFormat.Gif);
//bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
bmp.Dispose();
}
Image1.Src = paths[0];
//Check if there's more than 1 image cause its a TIFF
if (paths.Length>1)
{
Image2.Src = paths[1];
}
回答by configurator
actually i had been meet this problem, In my case, when i use IE browser, it is ok but when use another browser it always have the same error.
实际上我遇到过这个问题,就我而言,当我使用 IE 浏览器时,没问题,但是当使用其他浏览器时,它总是出现相同的错误。
"Parameter is not valid exception is always happening in the same line of code: System.Drawing.Image.FromStream(ms));"
“参数无效异常总是发生在同一行代码中:System.Drawing.Image.FromStream(ms));”
So i think it seems this issue depend on browser and type of image (JPEG,JPEG2000).
所以我认为这个问题似乎取决于浏览器和图像类型(JPEG、JPEG2000)。
回答by CRice
Here is some code I used converting bytes to an image for a unit test:
这是我用于将字节转换为图像以进行单元测试的一些代码:
protected override Image LoadImage()
{
//get a temp image from bytes, instead of loading from disk
//data:image/gif;base64,
byte[] bytes = Convert.FromBase64String("R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==");
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
return image;
}
回答by Tim Li
To my understanding, the image can not be shown because the format of the image's bytes is not correct. Every image format has its own head or something.
据我了解,图像无法显示,因为图像字节的格式不正确。每种图像格式都有自己的头部或其他东西。