正如@Simon Mourier和@Reza Aghaei在评论中所说,我不需要添加
CredentialCache
但是只有
Credentials
. 解决方案类似于
this one
.
解决方案:
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential(user, password);
MemoryStream imgStream = new MemoryStream(wc.DownloadData(mySnapUrl));//Good to go!
picturebox0.Image = new System.Drawing.Bitmap(imgStream);
编辑:
我个人必须能够异步加载上述图像,因为我以前加载图像时
picturebox0.LoadAsync(mySnapUrl)
.
我从中得到了一个大主意
source
.
为了能够使用需要凭据的图像,我创建了一个
async Task
要加载图像。。。
private async Task<Image> GetImageAsync(string snapUrl, string user, string password)
{
var tcs = new TaskCompletionSource<Image>();
Action actionGetImage = delegate ()
{
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential(user, password);
MemoryStream imgStream = new MemoryStream(wc.DownloadData(snapUrl));
tcs.TrySetResult(new System.Drawing.Bitmap(imgStream));
};
await Task.Factory.StartNew(actionGetImage);
return tcs.Task.Result;
}
... 然后使用以下命令设置图像:
var result = GetImageAsync(mySnapUrl, user, password);
result.ContinueWith(task =>
{
picturebox0.Image = task.Result;
});