【问题标题】:Download and Display an image in Windows 8在 Windows 8 中下载并显示图像
【发布时间】:2013-05-01 15:11:32
【问题描述】:

我有以下用于从网络服务器下载图像的代码:

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));
}

当我运行代码时,尽管图像出现在文件夹中(位于
C:\Users\User\AppData\Local\Packages\XXXXX\LocalState),但图像无法显示。

如果我再次运行它,我会在CreateFileAsync 收到UnauthorizedAccessException was unhandled by user codeAccess is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

有什么想法吗?

【问题讨论】:

  • 可能是“是否已插入”的答案之一,但您是否有权正确设置该文件夹?

标签: c# windows image windows-8 dotnet-httpclient


【解决方案1】:

这是一个应用程序的工作示例,它下载图像并将其显示为应用程序的背景:

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;
}

【讨论】:

    【解决方案2】:

    如果你不想下载图片,你可以这样做:

    private void displayImage()
    {
        image1.Source = new BitmapImage(
            new Uri("http://www.reignofcomputer.com/imgdump/sample.png"));
    }
    

    这有帮助吗?

    【讨论】:

    • sample.png其实是要换成动态图的,所以每次都要下载一个新的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 2011-09-09
    • 1970-01-01
    • 2019-02-17
    • 2023-03-10
    相关资源
    最近更新 更多