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

后端 未结 7 817
梦毁少年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:41

    Perhaps you should use the System.Net.WebClient API. If already using client.OpenRead(url) use client.DownloadData(url)

    var client = new System.Net.WebClient();
    byte[] buffer = client.DownloadData(url);
    using (var stream = new MemoryStream(buffer))
    {
        ... your code using the stream ...
    }
    

    Obviously this downloads everything before the Stream is created, so it may defeat the purpose of using a Stream. webClient.DownloadData("https://your.url") gets a byte array which you can then turn into a MemoryStream.

提交回复
热议问题