Very Basic Download Bitmap Image from the Web






1.33/5 (3 votes)
Download your Task asynchronously from the web.
Introduction
Downloads an image from the web asynchronously using an HttpClient.
Background
An HttpClient can be used to download most files from the web, in this case we are downloading the provided url and attempting to cast it to an image.
Using the code
Below is the code to get the Task<Bitmap>
. To use we can simply call FromWeb("web url of image here");
static async Task<Bitmap> FromWeb(string imageUrl)
{
using HttpClient client = new();
using MemoryStream imageStream = new
(
await client.GetByteArrayAsync(imageUrl)
);
return (Bitmap)Image.FromStream(imageStream);
}
Points of Interest
Change Task<Bitmap>
to Task<byte[]>
and return imageStream.ToArray();
for other file types.
History
V1. 08/25/2024