[C#] Bild mittels HttpWebRequest und HttpWebResponse herunterladen

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;

private long DownloadImage(string url)
{
    HttpWebRequest request = null;
    HttpWebResponse response = null;
    long contentlength = 0;
    
    try
    {
        request = (HttpWebRequest)WebRequest.Create(url);
        response = (HttpWebResponse)request.GetResponse();

        if ((response.StatusCode == HttpStatusCode.OK ||
             response.StatusCode == HttpStatusCode.Moved ||
             response.StatusCode == HttpStatusCode.Redirect) &&
             response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
        {
            Bitmap b = new Bitmap(response.GetResponseStream());

            // do something here
            ...

            b.Dispose();
            b = null;
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }
    finally
    {
        if (response != null)
        {
            contentlength = response.ContentLength;
            response.Close();
            response = null;
        }

        if (request != null)
        {
            request = null;
        }
    }
    
    // returns bytes read
    return contentlength;
}