Error “This stream does not support seek operations” in C#

后端 未结 7 840
梦毁少年i
梦毁少年i 2020-12-01 07:36

I\'m trying to get an image from an url using a byte stream. But i get this error message:

This stream does not support seek operations.<

7条回答
  •  广开言路
    2020-12-01 07:51

    With images, you don't need to read the number of bytes at all. Just do this:

    Image img = null;
    string path = "http://www.example.com/image.jpg";
    WebRequest request = WebRequest.Create(path);
    req.Credentials = CredentialCache.DefaultCredentials; // in case your URL has Windows auth
    WebResponse resp = req.GetResponse();
    
    using( Stream stream = response.GetResponseStream() ) 
    {
        img = Image.FromStream(stream);
        // then use the image
    }
    

提交回复
热议问题