我有一个带有图片的超链接。
我需要从该超链接中读取/加载图像,并将其分配给C#中的字节数组(byte[]
)。
谢谢。
WebClient.DownloadData
是最简单的方法。
string someUrl = "http://www.google.com/images/logos/ps_logo2.png";
using (var webClient = new WebClient()) {
byte[] imageBytes = webClient.DownloadData(someUrl);
}
using (var client = new HttpClient())
{
using (var response = await client.GetAsync(url))
{
byte[] imageBytes =
await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
}
}
.NET 4.5 引入了WebClient.DownloadDataTaskAsync()方法,用于异步使用。
示例:
using ( WebClient client = new WebClient() )
{
byte[] bytes = await client.DownloadDataTaskAsync( "https://someimage.jpg" );
}