1

我正在使用 HttpWebRequest 通过 SSL 连接到站点,下载图像,然后将字节转换为可以加载到图片框中的位图。

如果 Fiddler 打开,一切正常,但如果它关闭,我会收到错误Parameter is not valid。将 MemoryStream 传递给 Bitmap 构造函数时。

有什么想法吗?我认为这是 Fiddler 的证书在做某事,但为什么它会在 Fiddler 运行时工作?我从另一个位置(没有安装 Fiddler)尝试了该应用程序并得到了与上述相同的错误。

public static Bitmap ByteToImage(byte[] bytes)
{
   MemoryStream mStream = new MemoryStream();
   byte[] data = bytes;
   mStream.Write(data, 0, Convert.ToInt32(data.Length));
   Bitmap bm = new Bitmap(mStream, false);   //**Problem line**
   mStream.Dispose();
   return bm;
}
public static byte[] GetImageBytes()
{
   // CODE FOR HttpWebRequest HERE

   byte[] response;
   using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
   response = Parser.ResponseAsBytes(resp);
   return response;
}

public static byte[] ResponseAsBytes(HttpWebResponse response)
        {
            try
            {
                Stream stream;
                if (response == null || (stream = response.GetResponseStream()) == null || response.StatusCode != HttpStatusCode.OK) return null;
                byte[] bytes;
                using (stream)
                {
                    bytes = new byte[5000];
                    while ((stream.Read(bytes, 0, bytes.Length)) > 0) { }
                    return bytes;
                }
            }
            catch { return null; }
        }
4

1 回答 1

1

你可以这样尝试:

      System.Net.WebRequest request = System.Net.WebRequest.Create(YourURLString);
      System.Net.WebResponse resp = request.GetResponse();
      System.IO.Stream respStream = resp.GetResponseStream();
      Bitmap bmp = new Bitmap(respStream);
      respStream.Dispose();
      picturebox1.Image = bmp;
于 2013-07-16T02:38:17.490 回答