Easy Download and Save Images in C#

artydev - Jul 26 '23 - - Dev Community

Here is the code

using File = System.IO.File;

namespace Demo;
class Program
{
    public async static Task Main(string[] args)
    {
        string fileToWriteTo = "myimage.jpg";
        string urlImage = "https://g7y6h3a7.rocketcdn.me/wp-content/uploads/2020/03/dummy-image-square.jpg";
        using var httpClient = new HttpClient();
        await using var input = await httpClient.GetStreamAsync(urlImage);
        using Stream streamToWriteTo = File.Open(fileToWriteTo, FileMode.Create);
        await input.CopyToAsync(streamToWriteTo);
    }
}



Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .