C# 将字节[]转换为位图c时发生ArgumentException#
我正在尝试将字节数组转换为位图,但它始终显示: System.ArgumentException:参数无效 我的代码如下: 我正在通过以下Web服务传递字节:C# 将字节[]转换为位图c时发生ArgumentException#,c#,.net,asp.net,web-services,C#,.net,Asp.net,Web Services,我正在尝试将字节数组转换为位图,但它始终显示: System.ArgumentException:参数无效 我的代码如下: 我正在通过以下Web服务传递字节: string DecodedString = string.Empty; DecodedString = System.Text.Encoding.GetEncoding(1251).GetString(bytes); sResult = sResult + "<Photo>" +XmlConvert.EncodeNam
string DecodedString = string.Empty;
DecodedString = System.Text.Encoding.GetEncoding(1251).GetString(bytes);
sResult = sResult + "<Photo>" +XmlConvert.EncodeName(DecodedString) + "</Photo>";
尝试将字符串作为Base64传递:
string DecodedString = string.Empty;
DecodedString = System.Convert.ToBase64String(bytes)
sResult = sResult + "<Photo>" +XmlConvert.EncodeName(DecodedString) + "</Photo>";
您也不需要使用XmlConvert对字符串进行编码/解码。我最近遇到了类似的问题,但使用Silverlight。最后,我需要创建一个自定义HTTP处理程序,以便将定义图像的字节[]作为流传回 看 编辑:这样可以避免担心XML编码,并以二进制形式将图像传回。。。YMMV试试这个:
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);
编辑
看看这个:
根据:
ArgumentException-流没有有效的图像格式
我相信您的问题出在传递给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);
返回已读入字节数组的字节数;它并不总是读取整个文件尝试使用中的
StreamFile
方法。(第一个结果)我做到了,在大家的帮助下,这是我的页面代码
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];
}
byte[]bytes=System.Convert.FromBase64String(xDocument.SelectSingleNode(“响应/图像/照片”).InnerText);
System.IO.MemoryStream ms=新的System.IO.MemoryStream(字节);
System.Drawing.Bitmap b=新的System.Drawing.Bitmap(毫秒)//(位图)System.Drawing.Image.FromStream(ms);
System.Drawing.Imaging.FrameDimension frameDim;
frameDim=新系统.Drawing.Imaging.FrameDimension(b.FrameDimensionsList[0]);
int NumberOfFrames=b.GetFrameCount(frameDim);
字符串[]路径=新字符串[NumberOfFrames];
for(int i=0;i1)
{
Image2.Src=路径[1];
}
事实上,我曾经遇到过这个问题,在我的情况下,当我使用IE浏览器时,它是正常的,但当使用另一个浏览器时,它总是有相同的错误
“参数无效异常总是发生在同一行代码中:
System.Drawing.Image.FromStream(ms))
因此,我认为这个问题似乎取决于浏览器和图像类型(JPEG,JPEG2000)。以下是我在单元测试中将字节转换为图像时使用的一些代码:
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;
}
据我所知,图像无法显示,因为图像字节的格式不正确。每种图像格式都有自己的头或其他东西。但是如果我不使用enconde/decode,xml将无法读取,它将引发无效的xml异常。在这种情况下,不要在流周围放置using语句。调用Image.FromStream时,不能关闭流,否则图像将被破坏。当你处理图像时,这将关闭流。当我写答案的时候我很匆忙,实际上没有读完整的代码:/你从哪里得到字节[]?是否确定数组中的数据正确?字节来自:System.IO.FileStream fs=new System.IO.FileStream(sPath,System.IO.FileMode.Open,System.IO.FileAccess.Read);int streamLength=Convert.ToInt32(fs.Length);字节=新字节[streamLength];fs.Read(字节,0,流长度);在web服务中。现在,错误在此行中,其相同参数无效错误System.Drawing.Image Image=imageConverter.ConvertFrom(字节)为System.Drawing.Image;检查字节。长度,确保字节数组中确实有字节。检查后,仍然是相同的问题。字节来自:System.IO.FileStream fs=new System.IO.FileStream(sPath、System.IO.FileMode.Open、System.IO.FileAccess.Read);int streamLength=Convert.ToInt32(fs.Length);字节=新字节[streamLength];fs.Read(字节,0,流长度);在web服务中,您的字节数组有问题,我不认为它是一个有效的映像。好的,我会尝试一下,看看是否有效,但当我不使用web服务时,代码会自动运行,并且会显示映像。
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];
}
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;
}