sream to bytes []返回null。流是一个图像

时间:2017-06-11 09:05:50

标签: c# image stream byte

将图片Stream转换为byre[]始终返回null 这是代码:

    ...

    Stream Stream = responce.GetResponseStream();
    StreamReader reader = new StreamReader(Stream);
    string reader_str = reader.ReadToEnd();

    byte[] bytes = ReadFully(Stream); //I don't know why bytes is NULL

    public static byte[] ReadFully(Stream input)
    {
        byte[] buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
             int read;
             while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
             {
                  ms.Write(buffer, 0, read);
             }
             return ms.ToArray();
         }
     }

Stream是一张png图片!
如何保存下面的图像?

MemoryStream ms = new MemoryStream();
Stream.CopyTo(ms);
Image img = Image.FromStream(ms); //Parameter is not valid ERROR
img.Save(image_path, ImageFormat.Jpeg);

1 个答案:

答案 0 :(得分:1)

您已经通过reader.ReadToEnd();读取了结束的信息流。在再次从流中读取之前,您需要Seek()开头

reader_str的任何原因?