using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Net; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private static Dictionary cache = new Dictionary(); private static Task DownloadImageAsync(Uri uri) { var tcs = new TaskCompletionSource(); Image cachedImage; lock (cache) { if (cache.TryGetValue(uri, out cachedImage)) { tcs.SetResult(cachedImage); return tcs.Task; } } var wc = new WebClient(); wc.DownloadDataCompleted += (s, e) => { if (e.Cancelled) { tcs.SetCanceled(); } else if (e.Error != null) { tcs.SetException(e.Error); } else { Bitmap bmp; using (var stream = new MemoryStream(e.Result)) { bmp = new Bitmap(stream); } lock (cache) { if (!cache.ContainsKey(uri)) { cache.Add(uri, bmp); } } tcs.SetResult(bmp); } }; wc.DownloadDataAsync(uri); return tcs.Task; } private void button1_Click(object sender, EventArgs e) { var uri = new Uri("http://hoge/sample.png"); DownloadImageAsync(uri).ContinueWith(t => { pictureBox1.Image = t.Result; }); } } }