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<Uri, Image> cache = new Dictionary<Uri, Image>();
private static Task<Image> DownloadImageAsync(Uri uri)
{
var tcs = new TaskCompletionSource<Image>();
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;
});
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKdXNpbmcgU3lzdGVtLkRyYXdpbmc7CnVzaW5nIFN5c3RlbS5JTzsKdXNpbmcgU3lzdGVtLk5ldDsKdXNpbmcgU3lzdGVtLlRocmVhZGluZy5UYXNrczsKdXNpbmcgU3lzdGVtLldpbmRvd3MuRm9ybXM7CgpuYW1lc3BhY2UgV2luZG93c0Zvcm1zQXBwbGljYXRpb24xCnsKICAgIHB1YmxpYyBwYXJ0aWFsIGNsYXNzIEZvcm0xIDogRm9ybQogICAgewogICAgICAgIHB1YmxpYyBGb3JtMSgpCiAgICAgICAgewogICAgICAgICAgICBJbml0aWFsaXplQ29tcG9uZW50KCk7CiAgICAgICAgfQoKICAgICAgICBwcml2YXRlIHN0YXRpYyBEaWN0aW9uYXJ5PFVyaSwgSW1hZ2U+IGNhY2hlID0gbmV3IERpY3Rpb25hcnk8VXJpLCBJbWFnZT4oKTsKCiAgICAgICAgcHJpdmF0ZSBzdGF0aWMgVGFzazxJbWFnZT4gRG93bmxvYWRJbWFnZUFzeW5jKFVyaSB1cmkpCiAgICAgICAgewogICAgICAgICAgICB2YXIgdGNzID0gbmV3IFRhc2tDb21wbGV0aW9uU291cmNlPEltYWdlPigpOwogICAgICAgICAgICBJbWFnZSBjYWNoZWRJbWFnZTsKICAgICAgICAgICAgbG9jayAoY2FjaGUpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmIChjYWNoZS5UcnlHZXRWYWx1ZSh1cmksIG91dCBjYWNoZWRJbWFnZSkpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgdGNzLlNldFJlc3VsdChjYWNoZWRJbWFnZSk7CiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHRjcy5UYXNrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CgogICAgICAgICAgICB2YXIgd2MgPSBuZXcgV2ViQ2xpZW50KCk7CiAgICAgICAgICAgIHdjLkRvd25sb2FkRGF0YUNvbXBsZXRlZCArPSAocywgZSkgPT4KICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYgKGUuQ2FuY2VsbGVkKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIHRjcy5TZXRDYW5jZWxlZCgpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZSBpZiAoZS5FcnJvciAhPSBudWxsKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIHRjcy5TZXRFeGNlcHRpb24oZS5FcnJvcik7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgQml0bWFwIGJtcDsKICAgICAgICAgICAgICAgICAgICB1c2luZyAodmFyIHN0cmVhbSA9IG5ldyBNZW1vcnlTdHJlYW0oZS5SZXN1bHQpKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgYm1wID0gbmV3IEJpdG1hcChzdHJlYW0pOwogICAgICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICAgICAgbG9jayAoY2FjaGUpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBpZiAoIWNhY2hlLkNvbnRhaW5zS2V5KHVyaSkpCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGNhY2hlLkFkZCh1cmksIGJtcCk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgdGNzLlNldFJlc3VsdChibXApOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9OwoKICAgICAgICAgICAgd2MuRG93bmxvYWREYXRhQXN5bmModXJpKTsKICAgICAgICAgICAgcmV0dXJuIHRjcy5UYXNrOwogICAgICAgIH0KCiAgICAgICAgcHJpdmF0ZSB2b2lkIGJ1dHRvbjFfQ2xpY2sob2JqZWN0IHNlbmRlciwgRXZlbnRBcmdzIGUpCiAgICAgICAgewogICAgICAgICAgICB2YXIgdXJpID0gbmV3IFVyaSgiaHR0cDovL2hvZ2Uvc2FtcGxlLnBuZyIpOwogICAgICAgICAgICBEb3dubG9hZEltYWdlQXN5bmModXJpKS5Db250aW51ZVdpdGgodCA9PgogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBwaWN0dXJlQm94MS5JbWFnZSA9IHQuUmVzdWx0OwogICAgICAgICAgICB9KTsKICAgICAgICB9CiAgICB9Cn0K