【问题标题】:ArgumentException when converting byte[] to Bitmap c#将字节 [] 转换为位图 c# 时出现 ArgumentException
【发布时间】:2009-02-02 14:04:23
【问题描述】:

我正在尝试将字节数组转换为位图,但它总是显示:

System.ArgumentException:参数无效。

我的代码如下:

我通过网络服务传递字节:

 string DecodedString = string.Empty;
 DecodedString = System.Text.Encoding.GetEncoding(1251).GetString(bytes);
 sResult = sResult + "<Photo>" +XmlConvert.EncodeName(DecodedString) + "</Photo>";

在我的网页中:

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

【问题讨论】:

    标签: c# .net asp.net web-services


    【解决方案1】:

    尝试将字符串作为 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);
    

    您也不需要使用 XmlConvert 来编码/解码字符串。

    【讨论】:

    • 但是如果我不使用enconde/decode xml不能被读取它会抛出一个无效的xml异常..
    • 不要在这种情况下在流周围放置 using 语句。当您调用 Image.FromStream 时,您不得关闭流,否则图像将被破坏。当您处置图像时,that 将关闭流。
    • 哦。当我写答案并没有真正阅读整个代码时,我很着急:/
    【解决方案2】:

    在大家的帮助下我做到了,这是我的页面代码

     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];  
        }
    

    【讨论】:

      【解决方案3】:

      我最近遇到了类似的问题,但使用的是 Silverlight。我最终需要创建一个自定义 HTTP 处理程序,以便将定义图像的字节 [] 作为流传回。

      http://www.dotnetcurry.com/ShowArticle.aspx?ID=220

      编辑:这可以让您避免担心 XML 编码,并将图像以二进制形式传回... YMMV

      【讨论】:

      • 你从哪里得到字节[]?你确定数组中的数据是正确的吗?
      • 字节来自:System.IO.FileStream fs = new System.IO.FileStream(sPath, System.IO.FileMode.Open, System.IO.FileAccess.Read); int streamLength = Convert.ToInt32(fs.Length);字节=新字节[流长度]; fs.Read(bytes, 0, streamLength);在网络服务中。
      【解决方案4】:

      根据MSDN

      ArgumentException - 流没有有效的图像格式

      我相信您的问题出在您传递给 Web 服务的原始 byte[] 数组中。 根据您的一位 cmets,您做到了:

      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返回已读入字节数组的字节数;它并不总是读取整个文件!
      尝试使用http://www.dotnetspider.com/resources/4515-convert-file-into-byte-array.aspx 中的StreamFile 方法。 (Google search 的第一个结果)

      【讨论】:

      • 好的,我会试试这个,看看是否有效,但是当我不使用网络服务时,代码会运行并且图像会显示。
      【解决方案5】:

      试试这个:

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

      编辑

      看看这个:

      Transfer any files on Web services by c#

      【讨论】:

      • 现在错误出现在这一行,它的 Parameter is Not Valid Error System.Drawing.Image image = imageConverter.ConvertFrom(bytes) as System.Drawing.Image ;
      • 检查 bytes.Length,确保字节数组中确实有字节。
      • 检查过,还是同样的问题。字节来自: System.IO.FileStream fs = new System.IO.FileStream(sPath, System.IO.FileMode.Open, System.IO.FileAccess.Read); int streamLength = Convert.ToInt32(fs.Length);字节=新字节[流长度]; fs.Read(bytes, 0, streamLength);在网络服务中
      • 你的字节数组有问题,我不认为这是一个有效的图像。
      【解决方案6】:

      其实我也遇到过这个问题,在我的情况下,当我使用IE浏览器时,没问题,但是当使用另一个浏览器时,它总是有同样的错误。

      “参数无效异常总是发生在同一行代码中: System.Drawing.Image.FromStream(ms));"

      所以我认为这个问题似乎取决于浏览器和图像类型(JPEG、JPEG2000)。

      【讨论】:

        【解决方案7】:

        这是我用于将字节转换为图像以进行单元测试的一些代码:

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

        【讨论】:

          【解决方案8】:

          据我了解,图像无法显示,因为图像字节的格式不正确。每种图像格式都有自己的头部或其他东西。

          【讨论】:

            猜你喜欢
            • 2014-04-16
            • 2012-07-05
            • 2018-08-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-09-26
            相关资源
            最近更新 更多