[英]Download and Display an image in Windows 8
I have the following code for downloading an image from a webserver: 我有以下代码用于从网络服务器下载图像:
private async void Test_Click(object sender, RoutedEventArgs e)
{
HttpClient httpClient = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(
HttpMethod.Get, "http://www.reignofcomputer.com/imgdump/sample.png");
HttpResponseMessage response = await httpClient.SendAsync(request,
HttpCompletionOption.ResponseHeadersRead);
var imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
"sample.png", CreationCollisionOption.ReplaceExisting);
var fs = await imageFile.OpenAsync(FileAccessMode.ReadWrite);
DataWriter writer = new DataWriter(fs.GetOutputStreamAt(0));
writer.WriteBytes(await response.Content.ReadAsByteArrayAsync());
await writer.StoreAsync();
writer.DetachStream();
await fs.FlushAsync();
displayImage();
}
private void displayImage()
{
image1.Source = new BitmapImage(
new Uri("ms-appdata:///local/sample.png", UriKind.Absolute));
}
When I run the code, the image fails to display despite appearing in the folder (at 当我运行代码时,尽管出现在文件夹中(在
C:\\Users\\User\\AppData\\Local\\Packages\\XXXXX\\LocalState). C:\\用户\\用户\\应用程序数据\\本地\\套餐\\ XXXXX \\ LocalState)。
If I run it again, I get a UnauthorizedAccessException was unhandled by user code
, Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
如果再次运行它,我得到一个
UnauthorizedAccessException was unhandled by user code
, Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
at CreateFileAsync
. Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
在CreateFileAsync
。
Any ideas? 有任何想法吗?
Here's a working sample of an app that downloads an image and displays it as the background of the app: 这是一个应用程序的工作示例,该应用程序下载图像并将其显示为应用程序的背景:
http://laurencemoroney.azurewebsites.net/?p=247 http://laurencemoroney.azurewebsites.net/?p=247
async void doLoadBG()
{
System.Xml.Linq.XDocument xmlDoc = XDocument.Load(
"http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-US");
IEnumerable<string> strTest = from node in xmlDoc.Descendants("url")
select node.Value;
string strURL = "http://www.bing.com" + strTest.First();
Uri source = new Uri(strURL);
StorageFile destinationFile;
try
{
destinationFile = await ApplicationData.Current.LocalFolder
.CreateFileAsync(
"downloadimage.jpg", CreationCollisionOption.GenerateUniqueName);
}
catch (FileNotFoundException ex)
{
return;
}
BackgroundDownloader downloader = new BackgroundDownloader();
DownloadOperation download =
downloader.CreateDownload(source, destinationFile);
await download.StartAsync();
ResponseInformation response = download.GetResponseInformation();
Uri imageUri;
BitmapImage image = null;
if (Uri.TryCreate(destinationFile.Path, UriKind.RelativeOrAbsolute,
out imageUri))
{
image = new BitmapImage(imageUri);
}
imgBrush.ImageSource = image;
}
If you don't want to download the image, you can just do this: 如果您不想下载图像,则可以执行以下操作:
private void displayImage()
{
image1.Source = new BitmapImage(
new Uri("http://www.reignofcomputer.com/imgdump/sample.png"));
}
Does that help? 有帮助吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.